4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-configuration-processor
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
#redis的配置
spring:
redis:
host: 192.168.222.131
password: 123456
port: 6379
jedis:
pool:
max-active: 20
max-idle: 8
min-idle: 0
max-wait: 2000
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.*;
import java.util.Collection;
import java.util.List;
@SpringBootTest
class ApplicationTests {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void contextLoads() {
System.out.println(redisTemplate);
}
@Test
void flushdb(){
redisTemplate.execute(new RedisCallback() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushAll();
connection.flushDb();
return "ok";
}
});
}
@Test
void testNormal(){
redisTemplate.keys("*");
redisTemplate.multi();
redisTemplate.exec();
redisTemplate.watch("");
redisTemplate.unwatch();
redisTemplate.delete("k1");
Collection keys=null;
redisTemplate.delete(keys);
redisTemplate.randomKey();
redisTemplate.rename("oldKey","newKey");
redisTemplate.discard();
//
redisTemplate.getStringSerializer(); //指redis key序列化方式
redisTemplate.getValueSerializer(); //指值的序列化方式
redisTemplate.getHashKeySerializer();//指hash Vlaue的 key序列化方式 hset(key,key,value)
redisTemplate.getHashValueSerializer();//指hash Vlaue的 value序列化方式
}
@Test
void testString(){
ValueOperations opsForValue = redisTemplate.opsForValue();
//System.out.println(redisTemplate.getKeySerializer());
//System.out.println(redisTemplate.getValueSerializer());
//其它方法集
RedisOperations operations = opsForValue.getOperations();
// opsForValue.get("");
// opsForValue.set("","");
// opsForValue.setIfPresent("","");
// opsForValue.increment("");
// opsForValue.decrement("");
// opsForValue.set("name","xiaoming");
System.out.println(opsForValue.get("name"));
System.out.println("--------------------------");
System.out.println("操作完成");
}
@Test
void testList(){
ListOperations opsForList = this.redisTemplate.opsForList();
RedisOperations operations = opsForList.getOperations();
opsForList.leftPush("","");
opsForList.leftPushAll("","","","");
opsForList.rightPush("","");
opsForList.rightPushAll("","");
opsForList.leftPop("");
opsForList.rightPop("");
List key = opsForList.range("key", 0, -1);
}
@Test
void testHash(){
HashOperations opsForHash = this.redisTemplate.opsForHash();
opsForHash.put("","hashKey","value");
opsForHash.get("","hashKey");
}
@Test
void testSet(){
SetOperations opsForSet = this.redisTemplate.opsForSet();
opsForSet.add("","");
opsForSet.members("");
}
@Test
void testZset(){
ZSetOperations opsForZSet = this.redisTemplate.opsForZSet();
opsForZSet.add("key","value",1);
}
/**
* 集群操作
*/
@Test
void test(){
ClusterOperations clusterOperations = this.redisTemplate.opsForCluster();
}
}
package com.example.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer id;
private String name;
private String address;
private Date brith;
}
序列化接口
若不设置序列化规则,它将使用JDK自动的序列化将对象转换为字节,存到Redis 里面
它可以存在对象到redis里面
如果对象没有序列化,那么默认使用的JDK的序列化方式
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.Date;
@SpringBootTest
class ApplicationRedisTemplateTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
System.out.println(redisTemplate);
System.out.println(redisTemplate.getKeySerializer());
System.out.println(redisTemplate.getValueSerializer());
}
@Test
void testString(){
//这是设置key的序列化方式 因为RedisTemplate