zoukankan      html  css  js  c++  java
  • SpringBoot集成原生redis

    redis的使用之一是Spring-data-redis,前面有介绍。

    本篇介绍原生redis也就是jedis。这个效率更高

    1.maven引入依赖

    <!--springBoot-->
    <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
      <!--mysql-->
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
    </dependency>
    
      <!--druid pool-->
    <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid</artifactId>
       <version>1.0.5</version>
    </dependency>
    
      <!--redis-->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>
    
      <!--json-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.38</version>
    </dependency>
    
    

    2.在application.properties中配置数据源

    # mybatis
    #别名
    mybatis.type-aliases-package=com.imooc.miaosha.domain
    #mybatis默认是属性名和数据库字段名一一对应的,即
    #数据库表列:user_name
    #实体类属性:user_name
    #但是java中一般使用驼峰命名
    #数据库表列:user_name
    #实体类属性:userName
    #在Springboot中,可以通过设置map-underscore-to-camel-case属性为true来开启驼峰功能。
    #mybatis.configuration.mapUnderscoreToCamelCase属性为true也是开启驼峰,且优先级更高
    mybatis.configuration.mapUnderscoreToCamelCase=true
    mybatis.configuration.default-fetch-size=100
    mybatis.configuration.default-statement-timeout=3000
    #扫描映射文件
    mybatis.mapperLocations = classpath:com/imooc/miaosha/dao/*.xml
    # druid
    spring.datasource.url=jdbc:mysql://192.168.220.128:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.filters=stat
    spring.datasource.maxActive=2
    spring.datasource.initialSize=1
    spring.datasource.maxWait=60000
    spring.datasource.minIdle=1
    spring.datasource.timeBetweenEvictionRunsMillis=60000
    spring.datasource.minEvictableIdleTimeMillis=300000
    spring.datasource.validationQuery=select 'x'
    spring.datasource.testWhileIdle=true
    spring.datasource.testOnBorrow=false
    spring.datasource.testOnReturn=false
    spring.datasource.poolPreparedStatements=true
    spring.datasource.maxOpenPreparedStatements=20
    #redis
    redis.host=192.168.220.128
    redis.port=6379
    redis.timeout=3
    redis.password=123456
    redis.poolMaxTotal=10
    redis.poolMaxIdle=10
    redis.poolMaxWait=3
    
    

    3.封装Reids


    第一步:编写RedisConfig.java 加载配置的redis文件
    /**
     * 加载读取配置文件
     */
    @Component
    @ConfigurationProperties(prefix="redis")//扫描配置文件,提取以redis开头的属性
    public class RedisConfig {
       private String host;
       private int port;
       private int timeout;//秒,不是毫秒
       private String password;
       private int poolMaxTotal;
       private int poolMaxIdle;
       private int poolMaxWait;//秒
       public String getHost() {
          return host;
       }
       public void setHost(String host) {
          this.host = host;
       }
       public int getPort() {
          return port;
       }
       public void setPort(int port) {
          this.port = port;
       }
       public int getTimeout() {
          return timeout;
       }
       public void setTimeout(int timeout) {
          this.timeout = timeout;
       }
       public String getPassword() {
          return password;
       }
       public void setPassword(String password) {
          this.password = password;
       }
       public int getPoolMaxTotal() {
          return poolMaxTotal;
       }
       public void setPoolMaxTotal(int poolMaxTotal) {
          this.poolMaxTotal = poolMaxTotal;
       }
       public int getPoolMaxIdle() {
          return poolMaxIdle;
       }
       public void setPoolMaxIdle(int poolMaxIdle) {
          this.poolMaxIdle = poolMaxIdle;
       }
       public int getPoolMaxWait() {
          return poolMaxWait;
       }
       public void setPoolMaxWait(int poolMaxWait) {
          this.poolMaxWait = poolMaxWait;
       }
    }


    第二步:编写RedisPoolFactory.java获取redispool
    /**
     * 获取redisPo
     */
    @Service
    public class RedisPoolFactory {
    
       @Autowired
       RedisConfig redisConfig;
       
       @Bean
       public JedisPool JedisPoolFactory() {
          JedisPoolConfig poolConfig = new JedisPoolConfig();
          poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
          poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
          poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);
          JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
                redisConfig.getTimeout()*1000, redisConfig.getPassword(), 0);//database属性是redis是支持多个库的,默认16个库,索引从0开始
          return jp;
       }
       
    }

    第三步:定义redis Key的生成策略。一般使用interface+abstract+extends
    /**
     * key接口
     */
    public interface KeyPrefix {
          
       public int expireSeconds();//过期时间
       
       public String getPrefix();
       
    }
    
    
    /**
     * key的抽象类
     * 抽象类是可以定义私有的变量和方法,不可以对象
     * 接口里面都是公有的
     */
    public abstract class BasePrefix implements KeyPrefix{
       
       private int expireSeconds;//过期时间
       
       private String prefix;//key值
       
       public BasePrefix(String prefix) {//0代表永不过期
          this(0, prefix);
       }
       
       public BasePrefix( int expireSeconds, String prefix) {
          this.expireSeconds = expireSeconds;
          this.prefix = prefix;
       }
       
       public int expireSeconds() {//默认0代表永不过期
          return expireSeconds;
       }
    
       public String getPrefix() {
          String className = getClass().getSimpleName();
          return className+":" + prefix;//类名+
       }
    
    }
    
    /**
     * 用户类key实现
     */
    public class UserKey extends BasePrefix{
    
       private UserKey(String prefix) {
          super(prefix);
       }
       public static UserKey getById = new UserKey("id");
       public static UserKey getByName = new UserKey("name");
    }

    4.编写RedisService.java 提供redis服务,主要是get(),set(),exists(),序列化,反序列化,
    注意使用redis链接后 一定要归还到pool中。


    @Service
    public class RedisService {
       
       //注解推荐使用@Resource,需要导包
       @Autowired
       JedisPool jedisPool;
       
       /**
        * 获取当个对象
        * */
       public <T> T get(KeyPrefix prefix, String key,  Class<T> clazz) {
           Jedis jedis = null;
           try {
              jedis =  jedisPool.getResource();
              //生成真正的key
              String realKey  = prefix.getPrefix() + key;
              String  str = jedis.get(realKey);
              T t =  stringToBean(str, clazz);
              return t;
           }finally {
               returnToPool(jedis);
           }
       }
       
       /**
        * 设置对象
        * */
       public <T> boolean set(KeyPrefix prefix, String key,  T value) {
           Jedis jedis = null;
           try {
              jedis =  jedisPool.getResource();
              String str = beanToString(value);
              if(str == null || str.length() <= 0) {
                 return false;
              }
             //生成真正的key
              String realKey  = prefix.getPrefix() + key;
              int seconds =  prefix.expireSeconds();
              if(seconds <= 0) {//判断过期时间
                 jedis.set(realKey, str);
              }else {
                 jedis.setex(realKey, seconds, str);//设置key值,再设置一个过期时间
              }
              return true;
           }finally {
               returnToPool(jedis);
           }
       }
       
       /**
        * 判断key是否存在
        * */
       public <T> boolean exists(KeyPrefix prefix, String key) {
           Jedis jedis = null;
           try {
              jedis =  jedisPool.getResource();
             //生成真正的key
              String realKey  = prefix.getPrefix() + key;
             return  jedis.exists(realKey);
           }finally {
               returnToPool(jedis);
           }
       }
       
       /**
        * 增加值
        * */
       public <T> Long incr(KeyPrefix prefix, String key) {
           Jedis jedis = null;
           try {
              jedis =  jedisPool.getResource();
             //生成真正的key
              String realKey  = prefix.getPrefix() + key;
             return  jedis.incr(realKey);
           }finally {
               returnToPool(jedis);
           }
       }
       
       /**
        * 减少值
        * */
       public <T> Long decr(KeyPrefix prefix, String key) {
           Jedis jedis = null;
           try {
              jedis =  jedisPool.getResource();
             //生成真正的key
              String realKey  = prefix.getPrefix() + key;
             return  jedis.decr(realKey);
           }finally {
               returnToPool(jedis);
           }
       }
    
       /**
        * 序列化
        * @param value
        * @param <T>
        * @return
        */
       private <T> String beanToString(T value) {
          if(value == null) {
             return null;
          }
          Class<?> clazz = value.getClass();
          if(clazz == int.class || clazz == Integer.class) {
              return ""+value;
          }else if(clazz == String.class) {
              return (String)value;
          }else if(clazz == long.class || clazz == Long.class) {
             return ""+value;
          }else {
             return JSON.toJSONString(value);
          }
       }
    
       /**
        * 反序列化
        * @param str
        * @param clazz
        * @param <T>
        * @return
        */
       @SuppressWarnings("unchecked")
       private <T> T stringToBean(String str, Class<T> clazz) {
          if(str == null || str.length() <= 0 || clazz == null) {
              return null;
          }
          if(clazz == int.class || clazz == Integer.class) {
              return (T)Integer.valueOf(str);
          }else if(clazz == String.class) {
              return (T)str;
          }else if(clazz == long.class || clazz == Long.class) {
             return  (T)Long.valueOf(str);
          }else {
             return JSON.toJavaObject(JSON.parseObject(str), clazz);
          }
       }
    
       private void returnToPool(Jedis jedis) {
           if(jedis != null) {
              jedis.close();
           }
       }
    
    }


    5.在业务层调用 redisService,本篇从简,在controller调用,只是做一个测试。
    逻辑:先在redis判断是否存在key,存在则读取,否则从数据库读取,再存入redis。
    执行修改后value要覆盖原来的值
    @Controller
    @RequestMapping("/demo")
    public class SampleController {
    
       @Autowired
       UserService userService;
       
       @Autowired
       RedisService redisService;
       
       
        @RequestMapping("/redis/get")
        @ResponseBody
        public Result<User> redisGet() {
           User  user  = redisService.get(UserKey.getById, ""+1, User.class);
            return Result.success(user);
        }
        
        @RequestMapping("/redis/set")
        @ResponseBody
        public Result<Boolean> redisSet() {
           User user  = new User();
           user.setId(1);
           user.setName("1111");
           redisService.set(UserKey.getById, ""+1, user);//UserKey:id1
            return Result.success(true);
        }
        
        
    }
    
    
    
    
  • 相关阅读:
    驰骋工作流引擎-系统变量的引用
    驰骋工作流引擎-表单样本展示
    驰骋工作流引擎CCFLOW下载代码
    初识CSS
    初识HTML标签
    初识JDBC
    通过锁对象解决哲学家就餐问题
    MySQL基本用法
    LRU算法实现,HashMap与LinkedHashMap源码的部分总结
    Java简易实现记事本的打开与保存
  • 原文地址:https://www.cnblogs.com/zeussbook/p/9247326.html
Copyright © 2011-2022 走看看