Redis入门教程(一)
(一)Redis、Jedis、Spring Data Redis
Redis:是一款开源的K-V数据库,它是用C语言编写的,性能优越,遵守BSD协议。主要特点如下:
- Redis 支持数据持久化,采用Copy-on-write技术更新数据,可以异步保存到磁盘。
- 支持丰富的数据结构,如String、List、Hash、Set、Sorted Set。
- 所有操作都是原子性的,支持事务。
Jedis:是Redis官方的一款面向Java的客户端,为Java提供所需接口。
Spring Data Redis:Spring同样对Redis进行了整合,在Spring应用中,我们可以通过一些简单的配置来实现对Redis的访问,它对Redis的底层开发包高度封装,对Redis的操作主要通过RedisTemplate实现。
(二)How to use RedisTemplate
1、pom.xml导入依赖
<!--Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置application.properties
# Redis服务器连接端口
spring.redis.port=8387
# Redis服务器地址
spring.redis.host=139.135.140.45
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000ms
3、RedisTemplate的基本操作
//用@Autowired注入RedisTemplate
@Autowired
private RedisTemplate redisTemplate;
// 删除单个key和多个key
public void delete(String key){
redisTemplate.delete(key);
}
public void deleteKey (String ...keys){
redisTemplate.delete(keys);
}
//设置key失效时间及单位
public void setExpireTime(String key,long time){
redisTemplate.expire(key,time,TimeUnit.MINUTES);
}
// 根据key获取过期时间
public long getExpire(String key){
Long expire = redisTemplate.getExpire(key);
return expire;
}
// 判断key是否存在
public boolean hasKey(String key){
return redisTemplate.hasKey(key);
}
4、String类型相关操作
1)添加缓存和获取缓存值
//1、通过redisTemplate设置值
redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);
//通过redisTemplate获取-值
String str1 = (String)redisTemplate.boundValueOps("StringKey").get();
//2、通过BoundValueOperations设置值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringValue",1, TimeUnit.MINUTES);
//通过BoundValueOperations获取值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
String str2 = (String) stringKey.get();
//3、通过ValueOperations设置值
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
ops.set("StringValue","StringVaule",1, TimeUnit.MINUTES);
//通过ValueOperations获取值
ValueOperations ops = redisTemplate.opsForValue();
String str3 = (String) ops.get("StringKey");
2)设置过期时间
redisTemplate.boundValueOps("StringKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("StringKey",1,TimeUnit.MINUTES);
- 顺序递增递减
redisTemplate.boundValueOps("StringKey").increment(3L);
redisTemplate.boundValueOps("StringKey").increment(-3L);
5、Hash类型相关操作
1)添加缓存
//1、通过redisTemplate 设置值
redisTemplate.boundHashOps("HashKey").put("SmallKey","HashValue");
//2、通过BoundHashOperations 设置值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey","HashValue");
//3、通过HashOperations 设置值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey","SmallKey","HashValue");
2)设置过期时间
redisTemplate.boundValueOps("HashKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("HashKey",1,TimeUnit.MINUTES);
3)添加Map集合
HashMap<String,String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashmap);
4)提取HashKey里所有keys和value
//1、通过redisTemplate获取值
Set keys3 = redisTemplate.boundHashOps("HashKey").keys();
List value3 = redisTemplate.boundHashOps("HashKey").values();
//2、通过BoundHashOperations 获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys6 = hashKey.keys();
List value6 = hashKey.values();
//3、通过HashOperations 获取值
HashOperations hashOps = redisTemplate.opsForHash();
Set keys9 = hashOps.keys("HashKey");
List value9 = hashOps.values("HashKey");
5)根据key提取value
//1、通过redisTemplate获取key为SmallKey的value
String value1 = (String)redisTemplate.boundHashOps("HashKey").get("SmallKey");
//2、通过BoundHashOperations 获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
String value2=(String)hashKey.get("SmallKey");
//3、通过HashOperations 获取值
HashOperations hashOps = redisTemplate.opsForHash();
String value3 = (String)hashOps.get("HashKey","SmallKey")
6)获取所有键值对集合
//1、通过redisTemplate获取
Map entries1 = redisTemplate.boundHashOps("HashKey").entries();
//2、通过BoundHashOperations 获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Map entries2 = hashKey.entries();
//3、通过HashOperations 获取值
HashOperations hashOps = redisTemplate.opsForHash();
Map entries3 = hashOps.entries("HashKey");
7)删除
redisTemplate.boundHashOps("HashKey").delete("SmallKey");
redisTemplate.delete("HashKey");
8)判断Hash中是否含有SmallKey
Boolean bool= redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");