zoukankan      html  css  js  c++  java
  • Spring Boot 结合 Redis 缓存

    Redis官网:

    中:http://www.redis.cn/

    外:https://redis.io/

    redis下载和安装

    Redis官方并没有提供Redis的Windows版本,这里使用微软提供的:https://github.com/MicrosoftArchive/redis/releases

    Spring Boot 结合 Redis 缓存,可以使用网络上的Redis服务器,这样只需要在配置文件中设置Redis服务器地址,也可以在自己本地安装Redis,然后使用本地的Redis进行缓存

    直接使用服务器的Redis地址

    在配置文件中添加一下内容:

    spring:
      redis:
        # Redis数据库索引(默认为0)
        database: 0
        # Redis服务器地址
        host: ******
        # Redis服务器连接端口
        por: 6379
        # Redis服务器连接密码(默认为空)
        password:
        # 连接池最大连接数(使用负值表示没有限制)
        pool.max-active: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        pool.max-wait: -1
        # 连接池中的最大空闲连接
        pool.max-idle: 8
        # 连接池中的最小空闲连接
        pool.min-idle: 0
        # 连接超时时间(毫秒)
        timeout: 0

    这里我使用的yml方式的配置文件。

    Redis的安装下载:这里不多说了。

    使用本地Redis方式:

    Redis下载地址:下载ZIP格式

    https://github.com/MicrosoftArchive/redis/releases

    下载后解压

    然后运行redis-server.exe即可

    配置文件:

    @Configuration
    @EnableCaching
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
            RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
            template.setConnectionFactory(connectionFactory);
    
            //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
            Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
    
            ObjectMapper mapper = new ObjectMapper();
            mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            serializer.setObjectMapper(mapper);
    
            template.setValueSerializer(serializer);
            //使用StringRedisSerializer来序列化和反序列化redis的key值
            template.setKeySerializer(new StringRedisSerializer());
            template.afterPropertiesSet();
            return template;
        }
    }
      redis:
        # Redis数据库索引(默认为0)
        database: 0
        # Redis服务器地址
        host: localhost
        # Redis服务器连接端口
        por: 6379
        # Redis服务器连接密码(默认为空)
        password:
        # 连接池最大连接数(使用负值表示没有限制)
        pool:
          maxActive: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
          maxWait: -1
        # 连接池中的最大空闲连接
          maxIdle: 8
        # 连接池中的最小空闲连接
          minIdle: 0
        # 连接超时时间(毫秒)不要过于短暂
        timeout: 5000

    注解方式使用:

    注意,实体类需要实现序列化

  • 相关阅读:
    DataSingleton单例
    iOS平台XML解析类库对比和安装说明
    ASIHTTPRequest类库简介和使用说明
    IOS开发之格式化日期时间
    iOS多线程编程之NSThread的使用
    Django的Xadmin后台集成富文本Ueditor
    Django DEBUG=False后静态文件加载失败
    Python多进程
    linux平台虚拟环境依赖安装
    SQLite基础使用
  • 原文地址:https://www.cnblogs.com/jiangwz/p/8476932.html
Copyright © 2011-2022 走看看