zoukankan      html  css  js  c++  java
  • spring缓存配置

    配置redis缓存 并设置值序列化为json格式

    	//springboot会自动注入该bean
        @Bean
        public RedisConnectionFactory redisConnectionFactory() {
            RedisStandaloneConfiguration redisStandaloneConfiguration =
                    new RedisStandaloneConfiguration();
            redisStandaloneConfiguration.setHostName(host);
            redisStandaloneConfiguration.setPort(port);
            JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
            return jedisConnectionFactory;
        }
        
         @Bean
        public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
            //默认1
            RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
            config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()));
            config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));
            config = config.disableCachingNullValues();
            RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
                    .cacheDefaults(config)
                    .transactionAware()
                    .build();
            log.debug("自定义RedisCacheManager加载完成");
    
            return redisCacheManager;
        }
    
        //键得序列号
        private RedisSerializer<String> keySerializer() {
            return new StringRedisSerializer();
        }
    
        //值得序列号
        private RedisSerializer<Object> valueSerializer() {
            return new GenericJackson2JsonRedisSerializer();
        } 
    

    spring使用本地缓存配置 注:sprinboot导入缓存场景启动器自动为本地缓存 不需要该配置

        /**
         * 使用本地缓存
         */
        @Bean
        public CacheManager cacheManager(List<Cache> caches) {
            SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
            simpleCacheManager.setCaches(caches);
            return simpleCacheManager;
        }
    
        @Bean
        public ConcurrentMapCacheFactoryBean dictCkind() {
            ConcurrentMapCacheFactoryBean stockDetail = new ConcurrentMapCacheFactoryBean();
            // 如果用户设置名称为stockDetail的缓存,则需要添加这样的一个bean
            //stockDet
            return stockDetail;
        }
    

    spring中配置maven

              <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>2.3.4.RELEASE</version>
            </dependency>
    
  • 相关阅读:
    linux开机自动挂载NTFS-WINDOWS分区
    s3c6410学习笔记-烧写uboot+构建文件系统
    C++静态库与动态库(简介)
    linux下gmplayer安装(亲测OK!)
    得到IP包的数据意义(简单实现例子)
    IP校验和
    汇编三个数选最大
    单链表练习
    汇编写下strcpy
    排序好后写入文件
  • 原文地址:https://www.cnblogs.com/idcode/p/14551387.html
Copyright © 2011-2022 走看看