zoukankan      html  css  js  c++  java
  • spring boot redis代码与配置

    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.core.StringRedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @Configuration
    public class RedisConfig {
    
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Bean
        public RedisTemplate<String, String> redisTemplate(final RedisConnectionFactory factory) {
            final StringRedisTemplate template = new StringRedisTemplate(factory);
            final Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            final ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            template.setValueSerializer(jackson2JsonRedisSerializer);
            template.afterPropertiesSet();
            return template;
        }
    
    }
    /**
     * Created by pc on 2017/1/25.
     */
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.stereotype.Component;
    
    @Component
    public class RedisUtil {
        private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
        @Autowired
        private RedisTemplate redisTemplate;
    
        public RedisUtil() {
        }
    
        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 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 operations = this.redisTemplate.opsForValue();
            result = operations.get(key);
            return result;
        }
    
        public boolean set(String key, Object value) {
            boolean result = false;
    
            try {
                ValueOperations e = this.redisTemplate.opsForValue();
                e.set(key, value);
                result = true;
            } catch (Exception var5) {
                log.error("RedisUtil set exception", var5);
            }
    
            return result;
        }
    
        public boolean set(String key, Object value, Long expireTime) {
            boolean result = false;
    
            try {
                ValueOperations e = this.redisTemplate.opsForValue();
                e.set(key, value);
                this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
                result = true;
            } catch (Exception var6) {
                log.error("RedisUtil set exception", var6);
            }
    
            return result;
        }
    
        public boolean setIfAbsent(String key, Object value) {
            boolean result = false;
    
            try {
                ValueOperations e = this.redisTemplate.opsForValue();
                result = e.setIfAbsent(key, value).booleanValue();
            } catch (Exception var5) {
                log.error("RedisUtil setIfAbsent exception", var5);
            }
    
            return result;
        }
    
        public boolean setIfAbsent(String key, Object value, Long expireTime) {
            boolean result = false;
    
            try {
                ValueOperations e = this.redisTemplate.opsForValue();
                result = e.setIfAbsent(key, value).booleanValue();
                if(result) {
                    this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
                }
            } catch (Exception var6) {
                log.error("RedisUtil setIfAbsent exception", var6);
            }
    
            return result;
        }
    
        public boolean tryLock(String key, Long cacheSeconds) {
            boolean isLock = false;
    
            try {
                isLock = this.setIfAbsent(key, "", cacheSeconds);
            } catch (Exception var5) {
                log.error("RedisUtil tryLock exception", var5);
            }
    
            return isLock;
        }
    }
    # REDIS (RedisProperties)
    # Redis数据库索引(默认为0)
    spring.redis.database=0
    # Redis服务器地址
    spring.redis.host=192.168.128.131
    # Redis服务器连接端口
    spring.redis.port=6379
    # Redis服务器连接密码(默认为空)
    spring.redis.password=
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.pool.max-active=8
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.pool.max-wait=-1
    # 连接池中的最大空闲连接
    spring.redis.pool.max-idle=8
    # 连接池中的最小空闲连接
    spring.redis.pool.min-idle=0
    # 连接超时时间(毫秒)
    spring.redis.timeout=0
  • 相关阅读:
    luogu P1630 求和(枚举暴力)
    luogu P3414 SAC#1
    luogu P1869 愚蠢的组合数(质因数+瞎搞)
    luogu P1586 四方定理(背包)
    luogu P3795 钟氏映射(递推)
    2017.8.15 [Haoi2016]字符合并 区间dp+状压dp
    [NOI2002] 荒岛野人 扩展欧几里得算法
    [Noi2002]Savage 扩展欧几里得
    bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡
    bzoj 3505: [Cqoi2014]数三角形
  • 原文地址:https://www.cnblogs.com/fofawubian/p/6349812.html
Copyright © 2011-2022 走看看