redis-java中的callback回掉机制

springboot整合redis后, 会提供StringRedisTEmplate和 RedisTemplate 两个模板类供食用, 有时候这并不能满足我们的需求, 需要使用 connect 处理, 除了 redisTemplate.getConnection() 之外, 还可以使用callback机制进行处理

具体使用: 

 @Resource(name = "shardedJedisPool")  
    private ShardedJedisPool shardedJedisPool;  
      
    @Override  
    public <T> T execute(ConnectionCallback<T> action) {  
        ShardedJedis shardedJedis = null;  
        try{  
            // 从连接池中获取jedis分片对象  
            shardedJedis = shardedJedisPool.getResource();  
              
            return action.doInRedis(shardedJedis);  
              
        }catch (Exception e){  
            System.out.println(e.getMessage());  
        }finally {  
            if(null != shardedJedis){  
                shardedJedis.close();  
            }  
        }  
        return null;  
    }  
      
   /** 
     * attention:真正封装的方法,非常的简洁干脆 
     */  
    public String set(final String key, final String value){  
        return execute(new ConnectionCallback<String>() {  
            @Override  
            public String doInRedis(  
                    ShardedJedis shardedJedis) {  
                return shardedJedis.set(key, value);  
            }  
        });  
    }  
      
    public String get(final String key){  
        return execute(new ConnectionCallback<String>(){  
            @Override  
            public String doInRedis(ShardedJedis shardedJedis) {  
                return shardedJedis.get(key);  
            }  
        });  
    }  

保存: 

 redisTemplate.execute(new RedisCallback<Object>() {  
        @Override  
        public Object doInRedis(RedisConnection connection)  
                throws DataAccessException {  
            connection.set(  
                    redisTemplate.getStringSerializer().serialize(  
                            "user.uid." + user.getUid()),  
                    redisTemplate.getStringSerializer().serialize(  
                            user.getAddress()));  
            return null;  
        }  
    });  

获取: 

 return redisTemplate.execute(new RedisCallback<User>() {  
        @Override  
        public User doInRedis(RedisConnection connection)  
                throws DataAccessException {  
            byte[] key = redisTemplate.getStringSerializer().serialize(  
                    "user.uid." + uid);  
            if (connection.exists(key)) {  
                byte[] value = connection.get(key);  
                String address = redisTemplate.getStringSerializer()  
                        .deserialize(value);  
                User user = new User();  
                user.setAddress(address);  
                user.setUid(uid);  
                return user;  
            }  
            return null;  
        }  
    });  

删除: 

  redisTemplate.execute(new RedisCallback<Object>() {  
        public Object doInRedis(RedisConnection connection) {  
            connection.del(redisTemplate.getStringSerializer().serialize(  
                    "user.uid." + uid));  
            return null;  
        }  
    });  

 

 

说实话, 不知道比redistemplate 优在哪.. 有知道的可以跟我说下

 

posted @ 2017-06-13 10:26  bronk  阅读(12802)  评论(0编辑  收藏  举报