最终缓存API效果展示
缓存服务注册
第一步 创建工程,编写pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wq.cache</groupId>
<artifactId>wq-cache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wq-cache</name>
<description>缓存服务</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<swagger2.version>2.6.1</swagger2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- API文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger2.version}</version>
</dependency>
<!-- API文档 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 注册中心 -->
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步 编写配置文件application.yml
#缓存服务
server:
port: 1001
context-path: /wq-cache
#redis配置
spring:
redis:
database: 0 #用16个库中索引为0的库
host: 127.0.0.1 #redis服务器的主机IP
port: 6379 #redis的端口
password: #密码,默认为空
timeout: 1000 #连接超时时间,单位为毫秒
pool:
max-active: 200 #连接池的最大连接数,当为负数的时候表示没有限制
max-wait: -1 #连接池的最大阻塞等待时间,为负数的时候表示没有限制
max-idle: 10 #连接池的最大空闲连接
min-idle: 0 #连接池的最小的空闲连接
#spring应用配置
application:
name: wq-cache
#eureka配置
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: wq-cache
prefer-ip-address: true
#应用详细信息
info:
app.name: wq-cache
app.author: wei-qiang
app.date: 2018-08-27 21:00:00
第三步 swagger的配置
package com.wq.cache.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.wq.cache.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("缓存服务API文档")
.description("简单优雅的restfun风格")
.termsOfServiceUrl("http://localhost:1001/wq-cache/swagger-ui.html")
.version("1.0")
.build();
}
}
第四步 redis配置
package com.wq.cache.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wq.cache.util.RedisUtils;
/**
*author: wq
*datetime: 2018年8月27日 下午12:38:39
*E-mail: 1432114216@qq.com
*blog: https://blog.csdn.net/weiqiang2
*Description: Redis配置类
*
*/
@Configuration
@EnableCaching
public class RedisConfig {
private Logger logger = LoggerFactory.getLogger(RedisUtils.class);
@Bean
public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(factory);
@SuppressWarnings({ "rawtypes", "unchecked" })
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key序列化方式
redisTemplate.setKeySerializer(stringRedisSerializer);
//hash类型的key序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);
//value序列化方式jackson
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
logger.info("注入redisTemplate:"+redisTemplate);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisUtils redisUtils(RedisTemplate<String, Object> redisTemplate){
RedisUtils utils = new RedisUtils();
utils.setRedisTemplate(redisTemplate);
logger.info("注入RedisUtils:"+utils);
return utils;
}
}
第五步 RedisUtil工具类
package com.wq.cache.util;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
/**
*author: wq
*datetime: 2018年8月27日 下午12:38:39
*E-mail: 1432114216@qq.com
*blog: https://blog.csdn.net/weiqiang2
*Description:
*
*/
public class RedisUtils {
private Logger logger = LoggerFactory.getLogger(RedisUtils.class);
private RedisTemplate<String, Object> redisTemplate;
public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
//---------------------------------------------------时间设置/删除方法--------------------------------------------------------
/**
* 根据Key指定缓存失效的时间
* @param key
* @param time 失效时间,单位:毫秒
* @return
*/
public boolean expire(String key, long time){
try{
if(time >0){
this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
}catch(Exception e){
e.printStackTrace();
logger.error("redis设置 "+key+" 的缓存时间失败..."+e.getMessage());
return false;
}
return true;
}
/**
* 根据key获取指定的缓存时间
* @param key
* @return 返回 0 代表长期有效, 返回 -1 表示获取失败
*/
public long getExpire(String key){
if(key.trim() != null && key.trim() != "" ){
return -1;
}
try {
return this.redisTemplate.getExpire(key);
} catch (Exception e) {
e.printStackTrace();
logger.error("redis获取"+key+" 的缓存时间失败..."+e.getMessage());
return -1;
}
}
/**
* 判断key是否存在
* @param key
* @return 存在返回true,不存在则返回false
*/
public boolean hasKey(String key){
try {
return this.redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
logger.error("redis判断"+key+" 是否存在失败..."+e.getMessage());
return false;
}
}
/**
* 根据传来的key删除缓存,可以是一个key,也可以十多个
* @param key
*/
@SuppressWarnings("unchecked")
public boolean del(String...keys){
try {
if(keys.length > 0){
if(keys.length == 1){
this.redisTemplate.delete(keys[0]);
return true;
}else{
this.redisTemplate.delete(CollectionUtils.arrayToList(keys));
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("redis删除"+keys+" 失败..."+e.getMessage());
return false;
}
return false;
}
//--------------------------------------------------String类型的操作------------------------------------------------------------
/**
* 获取缓存
* @param key
* @return 当key为空的时候返回null
*/
public Object get(String key){
try {
return key == null ? null : this.redisTemplate.opsForValue().get(key);
} catch (Exception e) {
e.printStackTrace();
logger.error("redis获取"+key+" 的值失败..."+e.getMessage());
return null;
}
}
/**
* 普通设置缓存值 String<->Object
* @param key 键
* @param value 值
*/
public boolean set(String key, Object value){
try {
this.redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
logger.error("redis设置值失败..."+e.getMessage());
return false;
}
}
/**
* 普通缓存放入并设置时间
* @param key 键 String
* @param value 值 Object
* @param time 毫秒数 缓存时间
* @return
*/
public boolean set(String key, Object value, long time){
try {
this.redisTemplate.opsForValue().set(key, value, time);
return true;
} catch (Exception e) {
e.printStackTrace();
logger.error("redis设置 "+key+" 值失败..."+e.getMessage());
return false;
}
}
/**
* 递增
*
* @param key
* 键
* @param delta
* 要增加几(大于0)
* @return 失败返回 -1 成功返回增加之后的值
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
try {
return redisTemplate.opsForValue().increment(key, delta);
} catch (Exception e) {
e.printStackTrace();
logger.error("redis递增 "+key+" 值失败..."+e.getMessage());
return -1;
}
}
/**
* 递减
*
* @param key
* 键
* @param delta
* 要减少几(小于0)
* @return 成功返回递减之后的值,失败返回 -1
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
try {
return redisTemplate.opsForValue().increment(key, -delta);
} catch (Exception e) {
e.printStackTrace();
logger.error("redis递减 "+key+" 值失败..."+e.getMessage());
return -1;
}
}
//----------------------------------------------------Map类型的操作-----------------------------------------------------------
/**
* HashGet
*
* @param key
* 键 不能为null
* @param item
* 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
if(key == "" || key == null){
throw new RuntimeException("Key不能为null...");
}
if(item == "" || item == null){
throw new RuntimeException("item不能为null...");
}
try {
return redisTemplate.opsForHash().get(key, item);
} catch (Exception e) {
e.printStackTrace();
logger.error("redis获取 "+key+"---"+item+"值失败..."+e.getMessage());
return null;
}
}
/**
* HashSet
*
* @param key
* 键
* @param map
* 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
logger.error("redis设置 "+key+"---"+map+"值失败..."+e.getMessage());
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key
* 键
* @param map
* 对应多个键值
* @param time
* 时间(秒)
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* 键
* @param item
* 项
* @param value
* 值
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* 键
* @param item
* 项
* @param value
* 值
* @param time
* 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
*
* @param key
* 键 不能为null
* @param item
* 项 可以使多个 不能为null
*/
public boolean hdel(String key, Object... item) {
try {
redisTemplate.opsForHash().delete(key, item);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 判断hash表中是否有该项的值
*
* @param key
* 键 不能为null
* @param item
* 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
try {
return redisTemplate.opsForHash().hasKey(key, item);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key
* 键
* @param item
* 项
* @param by
* 要增加几(大于0)
* @return
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key
* 键
* @param item
* 项
* @param by
* 要减少记(小于0)
* @return
*/
public double hdecr(String key, String item, double by) {
try {
return redisTemplate.opsForHash().increment(key, item, -by);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ============================set=============================
/**
* 根据key获取Set中的所有值
*
* @param key
* 键
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key
* 键
* @param value
* 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入set缓存
*
* @param key
* 键
* @param values
* 值 可以是多个
* @return 成功个数
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将set数据放入缓存
*
* @param key
* 键
* @param time
* 时间(秒)
* @param values
* 值 可以是多个
* @return 成功个数
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0)
expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取set缓存的长度
*
* @param key
* 键
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值为value的
*
* @param key
* 键
* @param values
* 值 可以是多个
* @return 移除的个数
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ===============================list=================================
/**
* 获取list缓存的内容
*
* @param key
* 键
* @param start
* 开始
* @param end
* 结束 0 到 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取list缓存的长度
*
* @param key
* 键
* @return
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过索引 获取list中的值
*
* @param key
* 键
* @param index
* 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将list放入缓存
*
* @param key
* 键
* @param value
* 值
* @param time
* 时间(秒)
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* 键
* @param value
* 值
* @param time
* 时间(秒)
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* 键
* @param value
* 值
* @param time
* 时间(秒)
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* 键
* @param value
* 值
* @param time
* 时间(秒)
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据索引修改list中的某条数据
*
* @param key
* 键
* @param index
* 索引
* @param value
* 值
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N个值为value
*
* @param key
* 键
* @param count
* 移除多少个
* @param value
* 值
* @return 移除的个数
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}
第六步 服务层的开发
这里redis存在map,String,List,Set,Hash不同的操作,所以将不同的类型放在不同类中
RedisCommonOperationController
package com.wq.cache.controller.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.wq.cache.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
*author: wq
*datetime: 2018年8月27日 下午5:11:26
*E-mail: 1432114216@qq.com
*blog: https://blog.csdn.net/weiqiang2
*Description: Redis缓存服务 公共API
*
*/
@Api(value="Redis服务Common类型API")
@RestController
@RequestMapping("/cache/redis/common")
public class RedisCommonOperationController {
@Autowired
private RedisUtils redisUtils;
@ApiOperation(value="根据key设置缓存的过期时间", notes="参数说明:key为键值,time为过期时间")
@PutMapping("/expire")
public ResponseEntity<Boolean> expire(@RequestParam(value="key", required=true)String key,
@RequestParam(value="time", required=true)long time){
return ResponseEntity.ok(redisUtils.expire(key, time));
}
@ApiOperation(value="根据key获取缓存有效时间", notes="参数说明:key为键值,返回有效时间")
@GetMapping("/expire-time")
public ResponseEntity<Long> getExpire(@RequestParam(value="key", required=true)String key){
return ResponseEntity.ok(redisUtils.getExpire(key));
}
@ApiOperation(value="判断key是否存在", notes="参数说明:key为键值,存在返回true,否则false")
@GetMapping("/exist-key")
public ResponseEntity<Boolean> hasKey(@RequestParam(value="key", required=true)String key){
return ResponseEntity.ok(redisUtils.hasKey(key));
}
@ApiOperation(value="根据key删除缓存", notes="参数说明:keys可以是多个参数")
@DeleteMapping("/keys")
public ResponseEntity<Boolean> delKey(@RequestParam(value="keys", required=true)String...keys){
return ResponseEntity.ok(redisUtils.del(keys));
}
}
RedisListOperationController
package com.wq.cache.controller.redis;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.wq.cache.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
*author: wq
*datetime: 2018年8月27日 下午5:11:26
*E-mail: 1432114216@qq.com
*blog: https://blog.csdn.net/weiqiang2
*Description: Redis缓存服务 Set类型的API
*
*/
@Api(value="Redis服务List类型API")
@RestController
@RequestMapping("/cache/redis")
public class RedisListOperationController {
@Autowired
private RedisUtils redisUtils;
@ApiOperation(value="将Object放入缓存",
notes="参数说明:key->键;value->Object类型的值")
@PostMapping("/list")
public ResponseEntity<Boolean> lSet(@RequestParam(value="key", required=true)String key,
@RequestBody(required = true)Object value){
return ResponseEntity.ok(redisUtils.lSet(key,value));
}
@ApiOperation(value="将Object放入缓存,并设置过期时间",
notes="参数说明:key->键;value->Object类型的值;time->过期时间")
@PostMapping("/list-time")
public ResponseEntity<Boolean> lSet(@RequestParam(value="key", required=true)String key,
@RequestBody(required = true)Object value,
@RequestParam(value="time", required=true)long time){
return ResponseEntity.ok(redisUtils.lSet(key,value,time));
}
@ApiOperation(value="将list放入缓存",
notes="参数说明:key->键;value->Object类型的值")
@PostMapping("/list-list")
public ResponseEntity<Boolean> lSet(@RequestParam(value="key", required=true)String key,
@RequestBody(required = true)List<Object> value){
return ResponseEntity.ok(redisUtils.lSet(key,value));
}
@ApiOperation(value="将list放入缓存;并设置过期时间",
notes="参数说明:key->键;value->Object类型的值;time:long类型的毫秒数")
@PostMapping("/list-list-time")
public ResponseEntity<Boolean> lSet(@RequestParam(value="key", required=true)String key,
@RequestBody(required = true)List<Object> value,
@RequestParam(value="time", required=true)long time){
return ResponseEntity.ok(redisUtils.lSet(key,value,time));
}
@ApiOperation(value="移除N个值为value的项",
notes="参数说明:key->键;count->个数;value->Object类型的值")
@DeleteMapping("/list")
public ResponseEntity<Long> lRemove(@RequestParam(value="key", required=true)String key,
@RequestParam(value="count", required = true)long count,
@RequestBody(required = true)Object value){
return ResponseEntity.ok(redisUtils.lRemove(key,count,value));
}
@ApiOperation(value="获取list缓存的内容",
notes="参数说明:key->键;start->开始的索引;end->结束的索引;0到-1返回所有值;返回类型List<Object>")
@GetMapping("/list")
public ResponseEntity<List<Object>> sDecr(@RequestParam(value="key", required=true)String key,
@RequestParam(value="start", required = false)long start,
@RequestParam(value="end", required = false)long end){
return ResponseEntity.ok(redisUtils.lGet(key, start, end));
}
@ApiOperation(value="通过索引 获取list中的值",
notes="参数说明:key->键;index->索引;"
+ "索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推;返回Object")
@GetMapping("/list-index")
public ResponseEntity<Object> lGetIndex(@RequestParam(value="key", required=true)String key,
@RequestParam(value="index", required = true)long index){
return ResponseEntity.ok(redisUtils.lGetIndex(key, index));
}
@ApiOperation(value="获取list缓存的长度",
notes="参数说明:key->键;返回list长度")
@GetMapping("/list-length")
public ResponseEntity<Long> lGetListSize(@RequestParam(value="key", required=true)String key){
return ResponseEntity.ok(redisUtils.lGetListSize(key));
}
@ApiOperation(value="根据索引修改list中的某条数据",
notes="参数说明:key->键;index->索引;value->Object类型的值")
@PutMapping("/list")
public ResponseEntity<Boolean> lUpdateIndex(@RequestParam(value="key", required=true)String key,
@RequestParam(value="index", required = false)long index,
@RequestBody(required = true)Object value){
return ResponseEntity.ok(redisUtils.lUpdateIndex(key,index,value));
}
}
RedisMapOperationController
package com.wq.cache.controller.redis;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.wq.cache.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
*author: wq
*datetime: 2018年8月27日 下午5:11:26
*E-mail: 1432114216@qq.com
*blog: https://blog.csdn.net/weiqiang2
*Description: Redis缓存服务 Map类型的API
*
*/
@Api(value="Redis服务List类型API")
@RestController
@RequestMapping("/cache/redis")
public class RedisMapOperationController {
@Autowired
private RedisUtils redisUtils;
//-----------------------------------------------Map类型API---------------------------------------------------------------
@ApiOperation(value="根据key,item获取Hash类型的值", notes="参数说明:key为String,item为String类型")
@GetMapping("/map")
public ResponseEntity<Object> hget(@RequestParam(value="key", required=true)String key,
@RequestParam(value="item", required=true)String item){
return ResponseEntity.ok(redisUtils.hget(key,item));
}
@ApiOperation(value="根据key,item,设置Hash值为value", notes="参数说明:key为String,map为Map类型")
@PostMapping("/map-map")
public ResponseEntity<Boolean> hset(@RequestParam(value="key", required=true)String key,
@RequestParam(value="map", required=true)Map<String, Object> map){
return ResponseEntity.ok(redisUtils.hmset(key,map));
}
@ApiOperation(value="根据key,item,设置Hash值为value,并设置缓存过期事件", notes="参数说明:key为String,map为Map类型,time为long类型的毫秒")
@PostMapping("/map-map-time")
public ResponseEntity<Boolean> hset(@RequestParam(value="key", required=true)String key,
@RequestParam(value="map", required=true)Map<String, Object> map,
@RequestParam(value = "time")long time){
return ResponseEntity.ok(redisUtils.hmset(key,map,time));
}
@ApiOperation(value="向一张hash表中放入数据,如果不存在将创建;根据key,item,设置Hash值为value",
notes="参数说明:key为String,item为String类型,value为Object类型")
@PostMapping("/map-hash")
public ResponseEntity<Boolean> hset(@RequestParam(value="key", required=true)String key,
@RequestParam(value="item", required=true)String item,
@RequestBody(required = true)Object value){
return ResponseEntity.ok(redisUtils.hset(key,item,value));
}
@ApiOperation(value="向一张hash表中放入数据,如果不存在将创建;"
+ "根据key,item,设置Hash值为value,并设置过期时间",
notes="参数说明:key为String,item为String类型,value为Object类型,time为long类型的毫秒;"
+ "时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间")
@PostMapping("/map-hash-time")
public ResponseEntity<Boolean> hset(@RequestParam(value="key", required=true)String key,
@RequestParam(value="item", required=true)String item,
@RequestBody(required = true)Object value,
@RequestParam(value = "time", required = true)long time){
return ResponseEntity.ok(redisUtils.hset(key,item,value,time));
}
@ApiOperation(value="删除hash表中的值;key不能为null",
notes="参数说明:key为String,item为Object类型可以是多个item;")
@DeleteMapping("/map-hash")
public ResponseEntity<Boolean> hdel(@RequestParam(value="key", required=true)String key,
@RequestParam(value="item", required=true)Object... item){
return ResponseEntity.ok(redisUtils.hdel(key,item));
}
@ApiOperation(value="判断hash表中是否有该项的值",
notes="参数说明:key和item都不能为null")
@GetMapping("/map-key")
public ResponseEntity<Boolean> hHasKey(@RequestParam(value="key", required=true)String key,
@RequestParam(value="item", required=true)String item){
return ResponseEntity.ok(redisUtils.hHasKey(key,item));
}
@ApiOperation(value="hash递增 如果不存在,就会创建一个 并把新增后的值返回",
notes="参数说明:key->捡;item->项;by->增加多少;返回增加后的值")
@PutMapping("/map-adjunction")
public ResponseEntity<Double> hHasIncr(@RequestParam(value="key", required=true)String key,
@RequestParam(value="item", required=true)String item,
@RequestParam(value="by", required=true)double by){
return ResponseEntity.ok(redisUtils.hincr(key,item,by));
}
@ApiOperation(value="hash递减 如果不存在,就会创建一个 并把较少后的值返回",
notes="参数说明:key->捡;item->项;by->增加多少;返回增加后的值")
@PutMapping("/map-reduction")
public ResponseEntity<Double> hDecr(@RequestParam(value="key", required=true)String key,
@RequestParam(value="item", required=true)String item,
@RequestParam(value="by", required=true)double by){
return ResponseEntity.ok(redisUtils.hdecr(key,item,by));
}
}
RedisSetOperationController
package com.wq.cache.controller.redis;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.wq.cache.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
*author: wq
*datetime: 2018年8月27日 下午5:11:26
*E-mail: 1432114216@qq.com
*blog: https://blog.csdn.net/weiqiang2
*Description: Redis缓存服务 Set类型的API
*
*/
@Api(value="Redis服务Set类型API")
@RestController
@RequestMapping("/cache/redis")
public class RedisSetOperationController {
@Autowired
private RedisUtils redisUtils;
@ApiOperation(value="根据key获取Set中的所有值",
notes="参数说明:key->键;返回类型Set<Object>")
@GetMapping("/set")
public ResponseEntity<Set<Object>> sDecr(@RequestParam(value="key", required=true)String key){
return ResponseEntity.ok(redisUtils.sGet(key));
}
@ApiOperation(value="将数据放入set缓存,并设置时间",
notes="参数说明:key->键;values->每一项的值;返回成功放入的个数")
@PostMapping("/set")
public ResponseEntity<Long> sSet(@RequestParam(value="key", required=true)String key,
@RequestBody(required=true)Object... values){
return ResponseEntity.ok(redisUtils.sSet(key,values));
}
@ApiOperation(value="将数据放入set缓存,并设置过期时间",
notes="参数说明:key->键;values->每一项的值;time->long类型的过期时间;返回成功放入的个数")
@PostMapping("/set-time")
public ResponseEntity<Long> sSet(@RequestParam(value="key", required=true)String key,
@RequestParam(value="key", required=true)long time,
@RequestBody(required=true)Object... values){
return ResponseEntity.ok(redisUtils.sSet(key,values,time));
}
@ApiOperation(value="移除值为value的项",
notes="参数说明:key->键;values->每一项的值,可以为多个值;返回成功移除的个数")
@DeleteMapping("/set")
public ResponseEntity<Long> setRemove(@RequestParam(value="key", required=true)String key,
@RequestBody(required=true)Object... values){
return ResponseEntity.ok(redisUtils.setRemove(key,values));
}
@ApiOperation(value="根据value从一个set中查询,是否存在",
notes="参数说明:key->键;value->值")
@GetMapping("/set-key")
public ResponseEntity<Boolean> hHasKey(@RequestParam(value="key", required=true)String key,
@RequestBody(required=true)Object value){
return ResponseEntity.ok(redisUtils.sHasKey(key, value));
}
@ApiOperation(value="获取set缓存的长度",
notes="参数说明:key->键;返回key长度")
@GetMapping("/set-key-length")
public ResponseEntity<Long> sGetSetSize(@RequestParam(value="key", required=true)String key){
return ResponseEntity.ok(redisUtils.sGetSetSize(key));
}
}
RedisStringOperationController
package com.wq.cache.controller.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.wq.cache.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
*author: wq
*datetime: 2018年8月27日 下午5:11:26
*E-mail: 1432114216@qq.com
*blog: https://blog.csdn.net/weiqiang2
*Description: Redis缓存服务 Set类型的API
*
*/
@Api(value="Redis服务List类型API")
@RestController
@RequestMapping("/cache/redis")
public class RedisStringOperationController {
@Autowired
private RedisUtils redisUtils;
//-----------------------------------------String类型API--------------------------------------------------------------
@ApiOperation(value="根据key获取缓存", notes="参数说明:key为键,返回Object类型value")
@GetMapping("/string")
public ResponseEntity<Object> get(@RequestParam(value="key", required=true)String key){
return ResponseEntity.ok(redisUtils.get(key));
}
@ApiOperation(value="根据key和value设置缓存", notes="参数说明:key为String,value为Object类型")
@PostMapping("/string")
public ResponseEntity<Boolean> set(@RequestParam(value="key", required=true)String key,
@RequestBody(required = true) Object value){
return ResponseEntity.ok(redisUtils.set(key,value));
}
@ApiOperation(value="根据key,value,time设置缓存,并设置过期时间", notes="参数说明:key为String,value为Object类型,time为long类型的毫秒数")
@PostMapping("/string-time")
public ResponseEntity<Boolean> set(@RequestParam(value="key", required=true)String key,
@RequestParam(value="time", required=true)long time,
@RequestBody(required = true) Object value){
return ResponseEntity.ok(redisUtils.set(key,value,time));
}
@ApiOperation(value="根据key,delta递增", notes="参数说明:key为String,delta为long类型且不必须大于0")
@PutMapping("/string-adjunction")
public ResponseEntity<Long> incre(@RequestParam(value="key", required=true)String key,
@RequestParam(value="delta", required=true)long delta){
return ResponseEntity.ok(redisUtils.incr(key,delta));
}
@ApiOperation(value="根据key,delta递减", notes="参数说明:key为String,delta为long类型且不必须大于0")
@PutMapping("/string-reduction")
public ResponseEntity<Long> decr(@RequestParam(value="key", required=true)String key,
@RequestParam(value="delta", required=true)long delta){
return ResponseEntity.ok(redisUtils.decr(key,delta));
}
}