zoukankan      html  css  js  c++  java
  • redis整合spring(三)注解的方式缓存

    config

    @Configuration //
    @EnableCaching
    @EnableAspectJAutoProxy // 开启AOP自动代理
    public class AppConfig {
    
        @Value("${spring.redis.host}")
        String host;
    
        @Value("${spring.redis.port}")
        int port;
    
    
        // 创建对象,spring托管 <bean ...
        @Bean
        public JedisPool jedisPool() {
            JedisPool jedisPool = new JedisPool("localhost", 6379);
            return jedisPool;
        }
    
        @Bean
        public RedisTemplate redisTemplate() {
            RedisTemplate redisTemplate = new RedisTemplate();
            redisTemplate.setConnectionFactory(redisConnectionFactory());
            return redisTemplate;
        }
    
        @Bean
        public RedisConnectionFactory redisConnectionFactory() {
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
            redisStandaloneConfiguration.setHostName(host);
            redisStandaloneConfiguration.setPort(port);
            RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
            return redisConnectionFactory;
        }
    
        @Bean
        public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
            RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory());
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
            CacheManager cacheManager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
            return cacheManager;
        }
    
    }
    

      

    service

    @Service
    public class UserService {
    
        @Autowired
        JdbcTemplate jdbcTemplate; // spring提供jdbc一个工具(mybastis类似)
    
        @Autowired
        RedisTemplate redisTemplate;
    
        /**
         * 根据ID查询用户信息 (redis缓存,用户信息以json字符串格式存在(序列化))
         */
        @Cacheable(value="user",key="#userId") // 返回值 存到redis
        public User findUserById(String userId) {
            System.out.println("查询数据库.");
            String sql = "select * from tb_user_base where uid=?";
            User user = jdbcTemplate.queryForObject(sql, new String[]{userId}, new BeanPropertyRowMapper<>(User.class));
            return user;
        }
    
        @CacheEvict(value="user",key="#user.uid")  // 方法执行结束,清除缓存
        public void updateUser(User user){
            String sql = "update tb_user_base set uname=? where uid=?";
            jdbcTemplate.update(sql, new String[]{user.getUname(), user.getUid()});
        }
    
    }
    

      

      

      

  • 相关阅读:
    Qt之界面数据存储与获取(使用setUserData()和userData())
    UML中关联(Association)、聚合(Aggregation)和合成(Composition)之间的区别
    Entity Framework Model First下改变数据库脚本的生成方式
    keepalive学习
    函数、极限、连续
    C#集合基础与运用
    面向查询服务的参数化查询
    WinDbg 命令手册
    知识管理方法论
    项目管理Project
  • 原文地址:https://www.cnblogs.com/Jomini/p/13694015.html
Copyright © 2011-2022 走看看