目錄
一、Spring Data Redis使用方式
1.1?介紹? ? ? ?
1.2?配置?
1.3?RedisTemplate
二、環(huán)境搭建?
2.1?導(dǎo)入Spring Data Redis的maven坐標(biāo)
2.2?配置Redis數(shù)據(jù)源 ?
2.3?編寫配置類,創(chuàng)建RedisTemplate對象 ?
三、操作常見類型數(shù)據(jù)
3.1?操作字符串類型數(shù)據(jù)
3.2?操作哈希類型數(shù)據(jù) ?
3.3?操作列表類型數(shù)據(jù)
3.4?操作集合類型數(shù)據(jù)
3.5?操作有序集合類型數(shù)據(jù)
3.6?通用命令操作
一、Spring Data Redis使用方式
1.1?介紹? ? ? ?
Spring Data Redis 是 Spring 的一部分,提供了在 Spring 應(yīng)用中通過簡單的配置就可以訪問 Redis 服務(wù),對 Redis 底層開發(fā)包進(jìn)行了高度封裝。在 Spring 項(xiàng)目中,可以使用Spring Data Redis來簡化 Redis 操作。
網(wǎng)址:Spring Data Redis
1.2?配置?
Spring Boot提供了對應(yīng)的Starter,maven坐標(biāo):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1.3?RedisTemplate
Spring Data Redis中提供了一個高度封裝的類:RedisTemplate,對相關(guān)api進(jìn)行了歸類封裝,將同一類型操作封裝為operation接口,具體分類如下:
ValueOperations:string數(shù)據(jù)操作
SetOperations:set類型數(shù)據(jù)操作
ZSetOperations:zset類型數(shù)據(jù)操作
HashOperations:hash類型的數(shù)據(jù)操作
ListOperations:list類型的數(shù)據(jù)操作
二、環(huán)境搭建?
2.1?導(dǎo)入Spring Data Redis的maven坐標(biāo)
<dependency>
? ? ?<groupId>org.springframework.boot</groupId>
? ? ?<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.2?配置Redis數(shù)據(jù)源 ?
在application-dev.yml中添加:
sky:
? redis:
? ? host: localhost
? ? port: 6379
? ? password: 123456
? ? database: 10
解釋說明:
database:指定使用Redis的哪個數(shù)據(jù)庫,Redis服務(wù)啟動后默認(rèn)有16個數(shù)據(jù)庫,編號分別是從0到15。
可以通過修改Redis配置文件來指定數(shù)據(jù)庫的數(shù)量。
在application.yml中添加讀取application-dev.yml中的相關(guān)Redis配置
spring:
? profiles:
? ? active: dev
? redis:
? ? host: ${sky.redis.host}
? ? port: ${sky.redis.port}
? ? password: ${sky.redis.password}
? ? database: ${sky.redis.database}?
2.3?編寫配置類,創(chuàng)建RedisTemplate對象 ?
package com.sky.config;
import lombok.extern.slf4j.Slf4j;
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.StringRedisSerializer;
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("開始創(chuàng)建redis模板對象...");
RedisTemplate redisTemplate = new RedisTemplate();
//設(shè)置redis的連接工廠對象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//設(shè)置redis key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
解釋說明:
當(dāng)前配置類不是必須的,因?yàn)?Spring Boot 框架會自動裝配 RedisTemplate 對象,但是默認(rèn)的key序列化器為
JdkSerializationRedisSerializer,導(dǎo)致我們存到Redis中后的數(shù)據(jù)和原始數(shù)據(jù)有差別,故設(shè)置為文章來源:http://www.zghlxwxcb.cn/news/detail-627429.html
StringRedisSerializer序列化器。文章來源地址http://www.zghlxwxcb.cn/news/detail-627429.html
三、操作常見類型數(shù)據(jù)
3.1?操作字符串類型數(shù)據(jù)
/**
* 操作字符串類型的數(shù)據(jù)
*/
@Test
public void testString(){
// set get setex setnx
redisTemplate.opsForValue().set("name","小明");
String city = (String) redisTemplate.opsForValue().get("name");
System.out.println(city);
redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);
redisTemplate.opsForValue().setIfAbsent("lock","1");
redisTemplate.opsForValue().setIfAbsent("lock","2");
}
3.2?操作哈希類型數(shù)據(jù) ?
/**
* 操作哈希類型的數(shù)據(jù)
*/
@Test
public void testHash(){
//hset hget hdel hkeys hvals
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("100","name","tom");
hashOperations.put("100","age","20");
String name = (String) hashOperations.get("100", "name");
System.out.println(name);
Set keys = hashOperations.keys("100");
System.out.println(keys);
List values = hashOperations.values("100");
System.out.println(values);
hashOperations.delete("100","age");
}
?3.3?操作列表類型數(shù)據(jù)
/**
* 操作列表類型的數(shù)據(jù)
*/
@Test
public void testList(){
//lpush lrange rpop llen
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("mylist","a","b","c");
listOperations.leftPush("mylist","d");
List mylist = listOperations.range("mylist", 0, -1);
System.out.println(mylist);
listOperations.rightPop("mylist");
Long size = listOperations.size("mylist");
System.out.println(size);
}
3.4?操作集合類型數(shù)據(jù)
/**
* 操作集合類型的數(shù)據(jù)
*/
@Test
public void testSet(){
//sadd smembers scard sinter sunion srem
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c","d");
setOperations.add("set2","a","b","x","y");
Set members = setOperations.members("set1");
System.out.println(members);
Long size = setOperations.size("set1");
System.out.println(size);
Set intersect = setOperations.intersect("set1", "set2");
System.out.println(intersect);
Set union = setOperations.union("set1", "set2");
System.out.println(union);
setOperations.remove("set1","a","b");
}
3.5?操作有序集合類型數(shù)據(jù)
/**
* 操作有序集合類型的數(shù)據(jù)
*/
@Test
public void testZset(){
//zadd zrange zincrby zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset1","a",10);
zSetOperations.add("zset1","b",12);
zSetOperations.add("zset1","c",9);
Set zset1 = zSetOperations.range("zset1", 0, -1);
System.out.println(zset1);
zSetOperations.incrementScore("zset1","c",10);
zSetOperations.remove("zset1","a","b");
}
3.6?通用命令操作
/**
* 通用命令操作
*/
@Test
public void testCommon(){
//keys exists type del
Set keys = redisTemplate.keys("*");
System.out.println(keys);
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("set1");
for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(type.name());
}
redisTemplate.delete("mylist");
}
到了這里,關(guān)于Spring Data Redis:在Java中操作Redis的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!