zoukankan      html  css  js  c++  java
  • redis整合spring (一)

    1、

    @Configuration //
    public class AppConfig {
    
    
        // 创建对象,spring托管 <bean ...
        @Bean
        public JedisPool jedisPool() {
            JedisPool jedisPool = new JedisPool("localhost", 6379);
            return jedisPool;
        }
    } 

    2、根据查询用户 ID 信息(redis缓存,用户信息以json字符串保存(序列化) 

         以 string 的形式存储 

    @Service
    public class UserService {
    
        @Autowired
        JdbcTemplate jdbcTemplate; // spring提供jdbc一个工具(mybastis类似)
    
        @Autowired
        JedisPool jedisPool;
    
    
        /**
         * 根据ID查询用户信息 (redis缓存,用户信息以json字符串格式存在(序列化))
         */
        public User findUserById(String userId) {
            User user = null;
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                // 1、查询 redis 是否有数据
                String result = jedis.get(userId);
                if (result != null && "".equals(result)) {
                    // 把json 字符串转换为 User
                    user = (User) JSONObject.parse(result);
                    return user;  // 命中缓存
                }
    
                //2、查询数据库
                String sql = "select * from tb_user_base where uid=?";
                user = jdbcTemplate.queryForObject(sql, new String[]{userId}, new BeanPropertyRowMapper<>(User.class));
    
                // 3、将数据放入redis
                String userJsonStr = JSONObject.toJSONString(user);
                jedis.set(userId, userJsonStr);
    
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
            return user;
        }
    
    }
    

      以hash的形式存储

    /**
         * 根据ID查询用户信息 (redis缓存,用户信息以json字符串格式存在(序列化))
         */
        public User findUserById(String userId) {
            User user = null;
            Jedis jedis = null;
            try{
                jedis = jedisPool.getResource();
    
                // 1、 查询 redis 是否有数据 -- hash 方式
                Map<String, String> result = jedis.hgetAll(userId);
                if(result != null && "".equals(result)){
                    // map 对象转换为User
                    user = new User();
                    user.setAge(Integer.valueOf(result.get("age")));
                    user.setImg(result.get("img"));
                    user.setUname(result.get("uname"));
                    user.setUid(result.get("uid"));
                    return user;
                }
    
                //2、查询数据库
                String sql = "select * from tb_user_base where uid=?";
                user = jdbcTemplate.queryForObject(sql, new String[]{userId}, new BeanPropertyRowMapper<>(User.class));
    
                // 3、将数据放入redis
                String userJsonStr = JSONObject.toJSONString(user);
                HashMap<String, String> userInfo = new HashMap<>();
                userInfo.put("age", String.valueOf(user.getAge()));
                userInfo.put( "uname", String.valueOf(user.getUname()));
                userInfo.put( "uid", String.valueOf(user.getUid()));
                userInfo.put( "img", String.valueOf(user.getImg()));
                jedis.hmset(userId, userInfo);
    
            }finally {
                if(jedis != null){
                    jedis.close();
                }
            }
            return user;
        }
    
    
        /**
         * 根据ID查询用户名称
         */
        public String findUserNameById(String userId) {
            String uname = null;
            Jedis jedis = null;
            try{
                jedis = jedisPool.getResource();
                // 1、查询 redis 是否有数据
                uname = jedis.hget(userId, "uname");
                if(uname != null && "".equals(uname)){
                    return uname;  // 命中缓存
                }
    
                //2、查询数据库
                String sql = "select uname from tb_user_base where uid=?";
                uname = jdbcTemplate.queryForObject(sql, new String[]{userId}, String.class);
    
                // 3、将数据放入redis
                jedis.hset(userId, "uname", uname);
    
            }finally {
                if(jedis != null){
                    jedis.close();
                }
            }
            return uname;
        }
    

      

     github : https://github.com/szjomin/redisDemo/tree/master/redis/redis01 

      

  • 相关阅读:
    HTTP状态码及其含义
    c和python解决各种字符串反转问题的不同思路
    Python找出一串字符中出现最多的字符
    3个基本算法的实现技巧
    一种字符串搜索方法
    数据库开发经典总结
    apt、dpkg参数整理
    Python集合(set)类型的操作
    Python和Decorator(装饰器)模式
    Git使用基础
  • 原文地址:https://www.cnblogs.com/Jomini/p/13690847.html
Copyright © 2011-2022 走看看