zoukankan      html  css  js  c++  java
  • springboot集成redis

    1,添加依赖

    <!-- redis配置 begin -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.7.3</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.7.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
                <version>RELEASE</version>
            </dependency>
            <!-- redis配置 end -->

    2,添加配置信息

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

    3,Redis配置类

    package org.spring.web.component;
    import java.util.Arrays;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    /**
     * redis配置类
     */
    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport{
    
        @SuppressWarnings("rawtypes")
        @Bean
        public CacheManager cacheManager(RedisTemplate redisTemplate) {
            RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
            // 多个缓存的名称,目前只定义了一个
            rcm.setCacheNames(Arrays.asList("thisredis"));
            //设置缓存过期时间(秒)
            rcm.setDefaultExpiration(600);
            return rcm;
        }
    
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
            StringRedisTemplate template = new StringRedisTemplate(factory);
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            template.setValueSerializer(jackson2JsonRedisSerializer);
            template.afterPropertiesSet();
            return template;
        }
    
    }

    该类生成Redis操作类,并注入到容器中。

    4,验证

    像Redis中存入一个user对象

        @Override
        public String getUser() {
             User user=new User();
             user.setId(22);
             user.setUserName("interview");
             ValueOperations<String, User> operations=redisTemplate.opsForValue();
             operations.set("com.yfli", user);
             operations.set("com.neo.f", user,1,TimeUnit.SECONDS);
             try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }        
                //redisTemplate.delete("com.neo.f");
                boolean exists=redisTemplate.hasKey("com.neo.f");
                if(exists){
                    System.out.println("exists is true");
                }else{
                    System.out.println("exists is false");
                }     
                User redisUser=operations.get("com.yfli");
               return redisUser.toString();
        }

    redis大致有5中数据接口,字符串,hash,list,set,storedSet。目前的项目使用redis主要作为缓存。(相关问题:Redis缓存穿透?)

    Redis学习

  • 相关阅读:
    导航控制器生产,push,pop,root,index
    DNSserver内置笔记本
    解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
    ssh配置连接
    在UITouch事件中画圆圈-iOS8 Swift基础教程
    iOS之UITableViewCell左右滑动效果
    iOS UIView非常用方法及属性详解
    IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
    UIColor,CGColor,CIColor三者的区别和联系
    iOS中正确的截屏姿势
  • 原文地址:https://www.cnblogs.com/li-zhan/p/9391829.html
Copyright © 2011-2022 走看看