Redis简介
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;/*** 常规操作*/
@Service
public class RedisService {@Autowiredprivate RedisTemplate redisTemplate;// 批量删除对应的valuepublic void deleteAll(String... keys) {for (String key : keys) {delete(key);}}// 批量删除keypublic void deletePattern(String pattern) {Set<Serializable> keys = redisTemplate.keys(pattern);if (keys.size() > 0)redisTemplate.delete(keys);}// 删除指定key的valuepublic void delete(String key) {if (exists(key)) {redisTemplate.delete(key);}}// 判断缓存中是否有对应的valuepublic 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;/*** 直接操作String数据类型*/
@Service
public class StringRedisService {@Autowiredpublic 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);}// 删除某个keypublic void delete(String key) {stringRedisTemplate.delete(key);}// 判断某个key是否存在public boolean exist(String key) {return stringRedisTemplate.hasKey(key);}// 同redis命令的leftpushpublic void leftPush(String key, String value) {stringRedisTemplate.boundListOps(key).leftPush(value);}// 同redis命令的rightpoppublic 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
如果感觉不错的话请记得点赞哟!!!