redis-java中的callback回掉机制

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

具体使用: 

 @Resource(name = "shardedJedisPool")  
    private ShardedJedisPool shardedJedisPool;  
@Override  
</span><span style="color: rgba(0, 0, 255, 1)">public</span> &lt;T&gt; T execute(ConnectionCallback&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> action) {  
    ShardedJedis shardedJedis </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;  
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{  
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从连接池中获取jedis分片对象  </span>
        shardedJedis =<span style="color: rgba(0, 0, 0, 1)"> shardedJedisPool.getResource();  
          
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> action.doInRedis(shardedJedis);  
          
    }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e){  
        System.</span><span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)">.println(e.getMessage());  
    }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {  
        </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">null</span> !=<span style="color: rgba(0, 0, 0, 1)"> shardedJedis){  
            shardedJedis.close();  
        }  
    }  
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;  
}  

/
* 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);
}
});
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> String <span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)">(final String key){  
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> execute(<span style="color: rgba(0, 0, 255, 1)">new</span> ConnectionCallback&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">(){  
        @Override  
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String doInRedis(ShardedJedis shardedJedis) {  
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> shardedJedis.<span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)">(key);  
        }  
    });  
}  </span></pre>

保存: 

 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 优在哪.. 有知道的可以跟我说下