天天看點

spring-boot整合redis和lettuce

1.在項目pom中引人

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
<!--redis資料庫-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>           
  1. 在application.properties配置
#redis資料庫配置
#連接配接池最大連接配接數(使用負值表示沒有限制)
spring.redis.lettuce.pool.max-active=-1
#連接配接池中的最大空閑連接配接
spring.redis.lettuce.pool.max-idle=100
#連接配接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.lettuce.pool.max-wait=-1ms
#連接配接池中的最小空閑連接配接
spring.redis.lettuce.pool.min-idle=0
#redis資料庫連結配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0           

3.使用StringRedisTemplate操作redis

@Service
public class RedisService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 執行set操作
     *
     * @param key
     * @return
     */
    public void set(final String key, final String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 執行get操作
     * @param key
     * @return
     */
    public String get(final String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 執行delete操作
     *
     * @param key
     * @return
     */
    public boolean del(final String key) {
        return stringRedisTemplate.delete(key);
    }


    /**
     * 執行set操作并且設定生存時間,機關為:秒
     *
     * @param key
     * @param value //TimeUnit.SECONDS 秒
     *              //TimeUnit.MINUTES 分
     * @return
     */
    public void set(final String key, final String value, final Integer seconds) {
        stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
    }


    /**
     * 執行hset操作
     *
     * @param key
     * @param
     * @return
     */
    public void hset(final String key, final String mapkey, final String mapvalue) {
        stringRedisTemplate.opsForHash().put(key, mapkey, mapvalue);
    }

    /**
     * 執行hgetAll操作
     *
     * @param key
     * @param
     * @return
     */
    public Map<String, String> hgetAll(final String key) {
        return (Map)stringRedisTemplate.opsForHash().entries(key);
    }

    /**
     * 執行hdel操作
     *
     * @param key
     * @param
     * @return
     */
    public long hdel(final String key, final String[] strings) {
        return stringRedisTemplate.opsForHash().delete(key, strings);
    }


    /**
     * 執行hkeys操作
     *
     * @param
     * @param key
     * @return
     */
    public Set<String> hkeys(final String key) {
        return (Set) stringRedisTemplate.opsForHash().keys(key);
    }


    /**
     * 執行hvalues操作
     *
     * @param
     * @param key
     * @return
     */
    public List<String> hvalues(final String key) {
        return (List) stringRedisTemplate.opsForHash().values(key);
    }


    /**
     * 執行hget操作
     *
     * @param
     * @param key
     * @return
     */
    public String hget(final String key, final String mapkey) {
        return (String)stringRedisTemplate.opsForHash().get(key, mapkey);
    }

    /**
     * 執行hmset操作
     *
     * @param
     * @param key
     * @return
     */
    public void hmset(final String key, final Map<String, String> mapvalue) {
        stringRedisTemplate.opsForHash().putAll(key, mapvalue);
    }


    /**
     * 執行lpush操作
     * @param
     * @param key
     * @return
     */
    public long lpush(final String key,final String value) {
      return stringRedisTemplate.opsForList().leftPush(key,value);
    }

    /**
     * 執行lpop操作
     * @param
     * @param key
     * @return
     */
    public String lpop(final String key) {
        return stringRedisTemplate.opsForList().leftPop(key);
    }

    /**
     * 執行rpop操作
     * @param
     * @param key
     * @return
     */
    public String rpop(final String key) {
        return stringRedisTemplate.opsForList().rightPop(key);
    }

    /**
     * 執行list操作
     * 在清單中的尾部添加一個個值,傳回清單的長度
     *
     * @param key
     * @return
     */
    public Long rpush(final String key, final String value) {
        return stringRedisTemplate.opsForList().rightPush(key,value);
    }

    /**
     * 執行list操作
     * 在清單中的尾部添加多個值,傳回清單的長度
     *
     * @param key
     * @return
     */
    public Long rpush(final String key, final String[] value) {
        return stringRedisTemplate.opsForList().rightPushAll(key,value);
    }
    /**
     * 執行list操作
     * 擷取List清單
     *
     * @param key
     * @return
     */
    public List<String> lrange(final String key, final long start, final long end) {
        return stringRedisTemplate.opsForList().range(key,start,end);
    }


    /**
     * 執行list操作
     * 通過索引擷取清單中的元素
     *
     * @param key
     * @return
     */
    public String lindex(final String key, final long index) {
        return stringRedisTemplate.opsForList().index(key,index);
    }

    /**
     * 執行list操作
     * 擷取清單長度,key為空時傳回0
     *
     * @param key
     * @return
     */
    public Long llen(final String key) {
        return stringRedisTemplate.opsForList().size(key);
    }


    public boolean expire(final String key, final Integer seconds) {
        return stringRedisTemplate.expire(key,seconds,TimeUnit.SECONDS);
    }
}
           

4.如果對lettuce有更好的使用方式,請評論區溝通。