关于java:海星框架Redis的使用结合定时任务

36次阅读

共计 11073 个字符,预计需要花费 28 分钟才能阅读完成。

1. 引入 redis 依赖

在 common 下的 pom 文件中引入 Redis 依赖

<!--redis 依赖 -start-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
<!--redis 依赖 -end-->

2. 配置文件配置 Redis

在我的项目配置文件中配置 Redis

spring.redis.database=15
spring.redis.host=10.196.1.62
spring.redis.port=7019
spring.redis.password=gpSPcwzK
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=5s
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0

3. 设置 Redis

3.1 定义 Redis 的 key

在 common 文件夹下创立 cache 文件夹,定义 KeyConstans.java 文件

public class KeyConstans {
    
    /**
     * 存储预警类型编码与名称的对应关系
     * 初始化时执行 0
     */
    public static String DISTKEY = "SSCVHB:DISTLIST:ALARM";

    /**
     * 存储预警类型编码与父类名称的对应关系
     * 初始化时执行 0
     */
    public static String DISTPARENTKEY = "SSCVHB:DISTLIST:PARENT:ALARM";
    

}

3.2 设置 RedisConfig

在 cache 文件夹下定义 RedisConfig.java 文件


package com.hikvision.isc.common.cache;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {


/*    @Bean
    public JedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);
        config.setPassword(password);
        return new JedisConnectionFactory(config);
    }*/

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key 采纳 String 的序列化形式
        template.setKeySerializer(stringRedisSerializer);
        // hash 的 key 也采纳 String 的序列化形式
        template.setHashKeySerializer(stringRedisSerializer);
        // value 序列化形式采纳 jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash 的 value 序列化形式采纳 jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

3.3 定义 Redis 的 set、get 等办法

在 cache 文件夹下定义 RedisUtils.java 文件


package com.hikvision.isc.common.cache;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisUtils {private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取所有 key
     */
    public Set<String> getKeyAll() {Set<String> keySet = redisTemplate.keys("*");
        return keySet;
    }

    /**
     * 含糊搜寻 key
     */
    public Set<String> getKey(String key) {
        String s = "*" + key + "*";
        Set<String> keySet = redisTemplate.keys(s);
        return keySet;
    }


    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {e.printStackTrace();
        }
        return result;
    }


    /**
     * 写入缓存设置时效工夫
     * key 过期后,再获取 key 就是 null
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime, TimeUnit timeUnit) {
        boolean result = false;
        try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, timeUnit);
            result = true;
        } catch (Exception e) {e.printStackTrace();
        }
        return result;
    }

    /**
     * 设置 key 的过期工夫
     */
    public void expireKey(String key, Long l, TimeUnit timeUnit) {redisTemplate.expire(key, l, timeUnit);
    }


    /**
     * 批量删除对应的 value
     *
     * @param keys
     */

    public void remove(final String... keys) {for (String key : keys) {remove(key);
        }
    }


    /**
     * 批量删除 key
     *
     * @param pattern
     */

    public void removePattern(final String pattern) {Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0) {redisTemplate.delete(keys);
        }
    }


    /**
     * 删除对应的 value
     *
     * @param key
     */

    public void remove(final String key) {logger.info("redis 删除 key:{}", key);
        if (exists(key)) {redisTemplate.delete(key);
        }
    }


    /**
     * 判断缓存中是否有对应的 value
     *
     * @param key
     * @return
     */

    public boolean exists(final String key) {return redisTemplate.hasKey(key);
    }


    /**
     * 断缓存中是否有对应的 filed
     */

    public boolean hexists(final String key, String field) {return redisTemplate.opsForHash().hasKey(key, field);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */

    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }


    /**
     * 哈希 增加
     *
     * @param key
     * @param hashKey
     * @param value
     */

    public void hmSet(String key, Object hashKey, Object value) {HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }

    public void hmSet(String key, Object hashKey, Object value, Long expireTime, TimeUnit timeUnit) {HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
        redisTemplate.expire(key, expireTime, timeUnit);
    }


    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */

    public Object hmGet(String key, Object hashKey) {HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }

    /**
     * 获取 hashKey 对应的所有键值
     */
    public Map<String, String> hmGetMap(String key) {Map<String, String> map = redisTemplate.opsForHash().entries(key);
        return map;
    }


    /**
     * 列表增加, 单个元素增加
     *
     * @param k
     * @param v
     */

    public void lPush(String k, Object v) {ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(k, v);
    }
    /**
     * 列表取出, 单个元素取出
     * 应用办法时,redis 会同步删除汇合中的数据
     * @param k
     *
     */

    public Object lPop(String k) {ListOperations<String, Object> list = redisTemplate.opsForList();
       return   list.leftPop(k);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */

    public List<Object> lRange(String k, long l, long l1) {ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k, l, l1);
    }


    /**
     * 汇合增加
     *
     * @param key
     * @param value
     */

    public void add(String key, Object value) {SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }


    /**
     * 汇合获取
     *
     * @param key
     * @return
     */

    public Set<Object> setMembers(String key) {SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }


    /**
     * 有序汇合增加
     *
     * @param key
     * @param value
     * @param scoure
     */

    public void zAdd(String key, Object value, double scoure) {ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key, value, scoure);
    }


    /**
     * 有序汇合获取
     *
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */

    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }
}

4. 将数据存入 Redis 缓存中

在 service.impl 下创立 RedisDictInfoImpl.java
实现将数据库获取到的数据存入 Redis 缓存中
(数据库获取数据通过实体类和 mapper 即可)

package com.hikvision.isc.module.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hikvision.isc.common.cache.KeyConstans;
import com.hikvision.isc.common.cache.RedisUtils;
import com.hikvision.isc.module.business.entity.ibuilding.DictInfo;
import com.hikvision.isc.module.business.mapper.DictInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 存储预警类型编码与名称的对应关系
 * 存储预警类型编码与父类名称的对应关系
 */
@Service
public class RedisDictInfoImpl {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private DictInfoMapper dictInfoMapper;


    /** 增加到 redis 中 */
    public void cacheDict(){
        // 从数据库获取须要存入 Redis 缓存的数据
        QueryWrapper wrapper = new QueryWrapper();
        List<DictInfo> list = dictInfoMapper.selectList(wrapper);

        // 遍历 list,存入缓存——存储预警类型编码与名称的对应关系
        list.forEach(l->{if ("业余施工人员".equals(l.getDictName())){l.setDictName("施工人员");
            }
            // 调用 hmSet 办法将每一条对象存入存入 KeyConstans.DISTKEY 中
            redisUtils.hmSet(KeyConstans.DISTKEY, l.getDictCode(), l.getDictName());
        });
        redisUtils.hmSet(KeyConstans.DISTKEY, "0", "失常");

        /**
         * 存储预警类型编码与父类名称的对应关系
         */
        list.forEach(l1->{if (StringUtils.isEmpty(l1.getParentCode())){
                // 如果父级编码为 null, 就将编码和编码名称存入 KeyConstans.DISTPARENTKEY 中
                redisUtils.hmSet(KeyConstans.DISTPARENTKEY, l1.getDictCode(), l1.getDictName());
            }else {
                // 如果父级不为空,将父级编码名称与编码名称拼接存入 KeyConstans.DISTPARENTKEY 中
                String name = "-" + l1.getDictName();
                String parentName = (String) redisUtils.hmGet(KeyConstans.DISTKEY, l1.getParentCode());
                redisUtils.hmSet(KeyConstans.DISTPARENTKEY, l1.getDictCode(), parentName + name);
            }
        });
    }

}

5. 创立定时工作

在 business 文件夹下创立 init 文件夹

5.1 创立定时工作治理类

在 init 文件夹下创立:TaskManager.java 文件
应用注解:
@Component
@EnableScheduling

package com.hikvision.isc.module.business.init;

import com.hikvision.isc.module.business.service.impl.RedisDictInfoImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@EnableScheduling
public class TaskManager {private static Logger logger = LoggerFactory.getLogger(TaskManager.class);

    // 注入具体实现缓存的实现类
    @Autowired
    private RedisDictInfoImpl redisDictInfo;

    @Scheduled(cron = "自定义执行工夫")
    public void syncAlarmCodeAndName(){
        try {
            // 调用具体方法将数据写入缓存
            redisDictInfo.cacheDict();}catch (Exception e){logger.error("定时工作执行异样"+new Date(),e);
        }
        
    }
}

5.2 定义定时工作启动类

在 init 文件夹下创立:InitTask.java,实现 CommandLineRunner,重写 run 办法

package com.hikvision.isc.module.business.init;

import com.hikvision.isc.module.business.service.impl.RedisDictInfoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class InitTask implements CommandLineRunner {

    @Autowired
    private TaskManager taskManager;

    @Override
    public void run(String... args) throws Exception {

        // 我的项目启动时进行缓存
        taskManager.syncAlarmCodeAndName();}
}

6. 设置启动类

在启动类上增加注解:
@EnableScheduling

7. 编写测试 Controller 进行测试

在 business 下创立 controller 文件夹,创立 TestController.java 文件
调用 RedisUtils 类的 hmGet 办法传入指定 key,获取缓存中的 value

package com.hikvision.isc.module.business.controller;

import com.hikvision.isc.common.cache.KeyConstans;
import com.hikvision.isc.common.cache.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Api(tags = {"测试接口"})
public class TestController {

    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation("依据预警编码获取 redis 缓存中的预警名称")
    @RequestMapping(value = "/getDictName", method = RequestMethod.GET)
    public Object getDictName(@RequestParam String dictCode) {return redisUtils.hmGet(KeyConstans.DISTKEY, dictCode);
    }
}

8. 运行我的项目

运行我的项目测试:

正文完
 0