当前位置: 移动技术网 > IT编程>开发语言>Java > springboot + redis(单机版)

springboot + redis(单机版)

2018年08月19日  | 移动技术网IT编程  | 我要评论

  本次和大家分享的是在springboot集成使用redis,这里使用的是redis的jedis客户端(这里我docker运行的redis,可以参考 ),如下添加依赖:

<dependency>
  <groupid>redis.clients</groupid>
  <artifactid>jedis</artifactid>
</dependency>

  然后需要redis的相关配置(这里我的redis密码是空),在application.yml设置如:

spring:
  redis:
    single: 192.168.146.28:6378
    jedis:
      pool:
        max-idle: 8
        max-active: 8
        max-wait: 3000
    timeout: 3000
    password:

  这是redis的一般配置,具体调优可以设置这些参数,下面在jedisconfig类中读取这些设置:

 1  @value("${spring.redis.single}")
 2     private string strsinglenode;
 3 
 4     @value("${spring.redis.jedis.pool.max-idle}")
 5     private integer maxidle;
 6 
 7     @value("${spring.redis.jedis.pool.max-active}")
 8     private integer maxactive;
 9 
10     @value("${spring.redis.jedis.pool.max-wait}")
11     private integer maxawait;
12 
13     @value("${spring.redis.timeout}")
14     private integer timeout;
15 
16     @value("${spring.redis.password}")
17     private string password;

  有上面的配置,就需要有代码里面设置下,这里创建一个返回jedispoolconfig的方法

 1     /**
 2      * jedis配置
 3      *
 4      * @return
 5      */
 6     public jedispoolconfig getjedispoolconfig() {
 7         jedispoolconfig config = new jedispoolconfig();
 8         config.setmaxidle(maxidle); #最大空闲数
 9         config.setmaxwaitmillis(maxawait); #最大等待时间
10         config.setmaxtotal(maxactive); #最大连接数
11         return config;
12     }

  有了配置,接下来就创建jedispool,这里把jedispool托管到spring中

 1   /**
 2      * 获取jedispool
 3      *
 4      * @return
 5      */
 6     @bean
 7     public jedispool getjedispool() {
 8         jedispoolconfig config = getjedispoolconfig();
 9         system.out.println("strsinglenode:" + this.strsinglenode);
10         string[] nodearr = this.strsinglenode.split(":");
11 
12         jedispool jedispool = null;
13         if (this.password.isempty()) {
14             jedispool = new jedispool(
15                     config,
16                     nodearr[0],
17                     integer.valueof(nodearr[1]),
18                     this.timeout);
19         } else {
20             jedispool = new jedispool(
21                     config,
22                     nodearr[0],
23                     integer.valueof(nodearr[1]),
24                     this.timeout,
25                     this.password);
26         }
27         return jedispool;
28     }

  上面简单区分了无密码的情况,到此jedis的配置和连接池就基本搭建完了,下面就是封装使用的方法,这里以set和get为例;首先创建个jediscomponent组件,代码如下:

/**
 * created by administrator on 2018/8/18.
 */
@component
public class jediscomponent {

    @autowired
    jedispool jedispool;

    public boolean set(string key, string val) {
        jedis jedis = null;
        try {
            jedis = jedispool.getresource();
            return jedis.set(key, val).equalsignorecase("ok");
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public <t> boolean set(string key, t t) {
        string strjson = jacksonconvert.serilize(t);
        if (strjson.isempty()) {
            return false;
        }
        return this.set(key, strjson);
    }

    public string get(string key) {
        jedis jedis = null;
        try {
            jedis = jedispool.getresource();
            return jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public <t> t get(string key, class<t> tclass) {
        string strjson = this.get(key);
        return jacksonconvert.deserilize(strjson, tclass);
    }
}

  有了对jedis的调用封装,我们在controller层的测试用例如下:

 1   @autowired
 2     jediscomponent jedis;
 3 
 4     @getmapping("/setjedis/{val}")
 5     public boolean setjedis(@pathvariable string val) {
 6         return jedis.set("token", val);
 7     }
 8 
 9     @getmapping("/getjedis")
10     public string getjedis() {
11         return jedis.get("token");
12     }

  运行set和get的接口效果如:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网