spring boot redis RedisTemplate和ValueOperations对 string-value的操作

spring boot redis RedisTemplate和ValueOperations对 string-value的操作 

参照redisdoc文档的方法进行 打造 http://redisdoc.com/ 与文档中string对应

写出通用接口 与实现类 

完整demo连接 

//配置缓存 和序列化操作

 
  
@Configuration
@EnableCaching
public class Cache  extends CachingConfigurerSupport {

    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

   // @SuppressWarnings("rawtypes")
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        //设置缓存过期时间
        //rcm.setDefaultExpiration(60);//秒
        return rcm;
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public ValueOperations valueOperations(){
        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);
        redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate.opsForValue();
    }

} 
  

//接口
public interface RedisString{
    //设置
    public void set(String key,Object o);

    public void set(String key,Object o,String P,Long time);

    //psetex 这个命令和 SEObjectEX 命令相似,但它以毫秒为单位设置 key 的生存时间
    public void pSetEX(String key,Object o,long time);

    // 获取
    public Object get(String key);

    //在字符串 后面追加
    public void append(String key,Object o);

    // 减1
    public void decr(String key);

    //减k
    public void decrBy(String key,long k);

    public void decrBy(String key,double k);

    //递增1
    public void incr(String key);

    //递增n
    public void incrBy(String key,long k);

    public void incrBy(String key,double k);

    /* 加k*/
    public void increment(String key,long k);

    //获取 位为K的值
    public Boolean getBit(String key,long k);

    //返回 key 中字符串值的子字符串
    public String getRange(String key,long l,long r);

    //获取和设置
    public Object getSet(String key,Object o);

    //获取多个key 的值
    public List mGet(List list);

    //设置多个key 的值
    public  void mSet(Map map);

    //设置多个key 的值   要么都成功 要么都失败
    public Boolean mSetNX(Map map);


    //设置偏移量
    public Boolean setBit(String key,long k,Boolean b);

    //设置存活时间
    public void setEX(String key,Object o,long time);

    //当key不存在的时候操作
    public void setNX(String key,Object o,long time);

    //设置偏移量位k的地方开始赋值
    public void setRange(String key,Object o,Long k);

    //计算key的值的长度
    public long strlen(String key);
}
 
  

//实现类

@Service
public class RedisStringImpl implements RedisString {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ValueOperations opsForValue;

    //设置key的值
    @Override
    public void set(String key, Object o) {
        opsForValue.set(key,o);
    }

    @Override
    public void set(String key, Object o, String P, Long time) {
        if (P.equals("EX")){    //秒位单位
            opsForValue.set(key,o,time, TimeUnit.SECONDS);
        }else if(P.equals("PX")){   //毫秒
            opsForValue.set(key,o,time,TimeUnit.MILLISECONDS);
        }else if(P.equals("NX")){   //不存在的时候操作
            if (opsForValue.get(key)==null) opsForValue.set(key,o);
        }else if (P.equals("XX")){  //存在的时候操作
            if (opsForValue.get(key)!=null) opsForValue.set(key,o);
        }
    }

    //获取key的值
    @Override
    public Object get(String key) {
        return opsForValue.get(key);
    }

    //在字符串 后面追加
    @Override
    public void append(String key,Object o) {
        opsForValue.append(key,o.toString());
    }

    // 减1
    @Override
    public void decr(String key) {
        opsForValue.increment(key,-1);
    }

    //减k
    @Override
    public void decrBy(String key, long k) {
        opsForValue.increment(key,-k);
    }

    @Override
    public void decrBy(String key, double k) {
        opsForValue.increment(key,k);
    }

    //递增1
    @Override
    public void incr(String key) {
        opsForValue.increment(key,1);
    }

    //递增n
    @Override
    public void incrBy(String key, long k) {
        opsForValue.increment(key,k);
    }

    @Override
    public void incrBy(String key, double k) {
        opsForValue.increment(key,k);
    }

    // 加k
    @Override
    public void increment(String key, long k) {
        opsForValue.increment(key,-1);
    }

    //获取 位为K的值
    @Override
    public Boolean getBit(String key, long k) {
        return opsForValue.getBit(key,k);
    }

    @Override
    public String getRange(String key, long l, long r) {
        return opsForValue.get(key,l,r);
    }

    //获取和设置
    @Override
    public Object getSet(String key,Object o) {
        return opsForValue.getAndSet(key,o);
    }

    //获取多个key 的值
    @Override
    public List mGet(List list) {
        return opsForValue.multiGet(list);
    }

    //设置多个key 的值
    @Override
    public void mSet(Map map) {
        opsForValue.multiSet(map);
    }

    //设置多个key 的值   要么都成功 要么都失败
    @Override
    public Boolean mSetNX(Map map) {
        return opsForValue.multiSetIfAbsent(map);
    }

    //psetex 这个命令和 SEObjectEX 命令相似,但它以毫秒为单位设置 key 的生存时间
    @Override
    public void pSetEX(String key, Object o, long time) {
        opsForValue.set(key,o,time, TimeUnit.MILLISECONDS);
    }

    //设置偏移量
    @Override
    public Boolean setBit(String key, long k, Boolean b) {
        return opsForValue.setBit(key,k,b);
    }

    //设置存活时间 秒位单位
    @Override
    public void setEX(String key,Object o, long time) {
        opsForValue.set(key,o,time,TimeUnit.SECONDS);
    }

    //当key不存在的时候操作
    @Override
    public void setNX(String key,Object o, long time) {
        if (opsForValue.get(key)==null) opsForValue.set(key,o);
    }

    //设置偏移量位k的地方开始赋值
    @Override
    public void setRange(String key, Object o, Long k) {
        opsForValue.set(key,o,k);
    }

    //计算key的值的长度
    @Override
    public long strlen(String key) {
        return opsForValue.size(key);
    }


}
 
  
//测试

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    RedisString redisString;
    @RequestMapping("/set")
    public String set(User user){
       // redisString.set(user.getUsername(),user);
      //  System.out.println(user);    //输出 传入的对象

        redisString.set("yuwen",100);     //设置语文成绩
        System.out.println(redisString.get("yuwen")); //输出

        redisString.incr("yuwen");   //自增1
        System.out.println("自增1------"+redisString.get("yuwen")); //输出

        redisString.decr("yuwen");   //自减1
        System.out.println("自减1------"+redisString.get("yuwen")); //输出

        redisString.incrBy("yuwen",10);   //增加10
        System.out.println("增10------"+redisString.get("yuwen")); //输出

        redisString.decrBy("yuwen",10);   //减加10
        System.out.println("减10------"+redisString.get("yuwen")); //输出

        redisString.pSetEX("shuxue",50,1000); //设置数学成绩位1秒存在时间
        System.out.println("数学------"+redisString.get("shuxue")); //输出  用此类 下面get方法进行测试

        redisString.setEX("yingyu",100,10); //英语存活时间10秒
        
        return user.toString();
    }

    @RequestMapping("get")
    public String get(String key){
        System.out.println(redisString.get(key));
        return "success";
    }
}


 
  


 
  


你可能感兴趣的:(redis)