zoukankan      html  css  js  c++  java
  • 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 优在哪.. 有知道的可以跟我说下

  • 相关阅读:
    利用web前端综合制作一个注册功能
    使用 kubeadm 快速部署一个 Kubernetes 集群
    部署一套完整的Kubernetes高可用集群(二进制,最新版v1.18)下
    ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'gitee.com...'
    fatal: unable to access 'https://gitee.com/...': Could not resolve host: gitee.com
    Qt学生管理系统
    Qt5.14.2生成qsqlmysql.lib
    express框架实现数据库CRUD操作
    链表常见的题型和解题思路
    2 引用
  • 原文地址:https://www.cnblogs.com/wenbronk/p/6999771.html
Copyright © 2011-2022 走看看