springboot集成redis

1、在Maven pom.xml文件中加入Redis包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、SpringBoot配置文件中配置Redis连接

#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=2
spring.redis.password=
spring.redis.jedis.pool.max-idle=100
spring.redis.jedis.pool.max-active=300
spring.redis.jedis.pool.max-wait=10000
spring.redis.timeout=10000
3、Redis配置类

@Configuration
public class RedisConfig {

    @Bean()
    @SuppressWarnings("all")
    public RedisTemplate<Serializable, Object> redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate<Serializable, Object> template = new RedisTemplate<Serializable, Object>();

        template.setConnectionFactory(factory);

        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();

        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(jdkSerializationRedisSerializer);

        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jdkSerializationRedisSerializer);

        template.afterPropertiesSet();

        return template;

    }
}

4、工具类JedisUtil

@Component
public class JedisUtil {
    private Logger logger = LoggerFactory.getLogger(RedisUtil.class);

    @Autowired
    private RedisTemplate<Serializable, Object> redisTemplate;

    public JedisUtil() {
    }

    public void remove(String... keys) {
        String[] var2 = keys;
        int var3 = keys.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String key = var2[var4];
            this.remove(key);
        }

    }

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

    }

    public void remove(String key) {
        if (this.exists(key)) {
            this.redisTemplate.delete(key);
        }

    }

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

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

    public boolean set(String key, Object value) {
        boolean result = false;

        try {
            ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return result;
    }

    public boolean set(String key, Object value, Long expireTime) {
        boolean result = false;

        try {
            ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
            operations.set(key, value, expireTime.longValue(), TimeUnit.SECONDS);
            result = true;
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return result;
    }

    public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public boolean hset(String key, String hashKey, Object value, Long expireTime) {
        boolean result = false;

        try {
            HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash();
            operations.put(key, hashKey, value);
            this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
            result = true;
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return result;
    }

    public boolean hset(String key, String hashKey, Object value) {
        boolean result = false;

        try {
            HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash();
            operations.put(key, hashKey, value);
            result = true;
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return result;
    }

    public Object hget(String key, String hashKey) {
        Object result = null;
        HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash();
        result = operations.get(key, hashKey);
        return result;
    }

    public boolean hdel(String key, String hashKey) {
        boolean result = false;

        try {
            HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash();
            operations.delete(key, new Object[]{hashKey});
            result = true;
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return result;
    }

    public Set<String> hgetAllKeys(String key) {
        Set<String> result = null;
        HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash();
        result = operations.keys(key);
        return result;
    }

    public List<Object> hgetAllValues(String key) {
        List<Object> result = null;
        HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash();
        result = operations.values(key);
        return result;
    }

    public Map<String, Object> hgetAll(String key) {
        Map<String, Object> result = null;
        HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash();
        result = operations.entries(key);
        return result;
    }

    public void lpush(String key, Object value) {
        ListOperations<Serializable, Object> operations = this.redisTemplate.opsForList();
        operations.leftPush(key, value);
    }

    public Object rpop(String key) {
        ListOperations<Serializable, Object> operations = this.redisTemplate.opsForList();
        return operations.rightPop(key);
    }

    public Object index(String key, int index) {
        ListOperations<Serializable, Object> operations = this.redisTemplate.opsForList();
        return operations.index(key, (long)index);
    }

    public Long increment(String key) {
        return this.redisTemplate.opsForValue().increment(key, 1L);
    }

    public Long increment(String key, long delta) {
        return this.redisTemplate.opsForValue().increment(key, delta);
    }

    public RedisTemplate getRedisTemplate() {
        return this.redisTemplate;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值