GitHub

src="//ghbtns/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">

简介

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

存储类型

和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。

数据追加方式

在此基础上,redis支持各种不同方式的排序。与Memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

添加jar包依赖集成Redis

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

RedisService常规操作

package com.jege.spring.boot.service;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @author JE哥
 * @email 1272434821@qq
 * @description:常规操作
 */
@Service
public class RedisService {
  @Autowired
  private RedisTemplate redisTemplate;

  // 批量删除对应的value
  public void deleteAll(String... keys) {
    for (String key : keys) {
      delete(key);
    }
  }

  // 批量删除key
  public void deletePattern(String pattern) {
    Set<Serializable> keys = redisTemplate.keys(pattern);
    if (keys.size() > 0)
      redisTemplate.delete(keys);
  }

  // 删除指定key的value
  public void delete(String key) {
    if (exists(key)) {
      redisTemplate.delete(key);
    }
  }

  // 判断缓存中是否有对应的value
  public boolean exists(String key) {
    return redisTemplate.hasKey(key);
  }

  // 读取缓存
  public Object get(String key) {
    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
    return operations.get(key);
  }

  // 写入缓存
  public boolean set(String key, Object value) {
    boolean flag = false;
    try {
      ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
      operations.set(key, value);
      flag = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return flag;
  }

  // 写入缓存
  public boolean set(String key, Object value, Long expireTime) {
    boolean flag = false;
    try {
      ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
      operations.set(key, value);
      redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
      flag = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return flag;
  }
}

StringRedisService

package com.jege.spring.boot.service;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @author JE哥
 * @email 1272434821@qq
 * @description:直接操作String数据类型
 */
@Service
public class StringRedisService {
  @Autowired
  public StringRedisTemplate stringRedisTemplate;

  // 获取某个key的剩余过期时间
  public long residualExpirationTime(String key) {
    return stringRedisTemplate.getExpire(key);
  }

  // 当key不存在时,为key赋值
  public boolean setValue(String key, String value) {
    ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
    return ops.setIfAbsent(key, value);
  }

  // 为key赋值,同时设置过期时间
  public void set(String key, String value, long time) {
    BoundValueOperations<String, String> ops = stringRedisTemplate.boundValueOps(key);
    ops.set(value, time, TimeUnit.SECONDS);
  }

  // 删除某个key
  public void delete(String key) {
    stringRedisTemplate.delete(key);
  }

  // 判断某个key是否存在
  public boolean exist(String key) {
    return stringRedisTemplate.hasKey(key);
  }

  // 同redis命令的leftpush
  public void leftPush(String key, String value) {
    stringRedisTemplate.boundListOps(key).leftPush(value);
  }

  // 同redis命令的rightpop
  public String rightPop(String key) {
    return stringRedisTemplate.boundListOps(key).rightPop();
  }
}

application.properties

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-idle=100 
spring.redis.pool.min-idle=1
spring.redis.pool.max-active=1000
spring.redis.pool.max-wait=-1

其他关联项目

  • Spring Boot 菜鸟教程 14 动态修改定时任务cron参数
    http://blog.csdn/je_ge/article/details/53434321

源码地址

https://github/je-ge/spring-boot

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!

更多推荐

Spring Boot 菜鸟教程 22 Redis