spring boot redis RedisTemplate 와 Value Operations 의 string - value 동작

9390 단어 redis
spring boot redis RedisTemplate 와 Value Operations 의 string - value 동작 
redisdoc 문서 의 방법 을 참조 하여 만 듭 니 다. http://redisdoc.com/ 문서 의 string 과 대응
유 니 버 설 인터페이스 와 구현 클래스 쓰기 
전체 데모 연결 
/ / 캐 시 와 직렬 화 작업 설정
 
  
@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";
    }
}


 
  


 
  


좋은 웹페이지 즐겨찾기