mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring boot 使用 redisson 示例
This commit is contained in:
62
lab-11/lab-07-spring-data-redis-with-redisson/pom.xml
Normal file
62
lab-11/lab-07-spring-data-redis-with-redisson/pom.xml
Normal file
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-07-spring-data-redis-with-redisson</artifactId>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- 实现对 Spring Data Redis 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
<version>3.11.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 方便等会写单元测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 等会示例会使用 fastjson 作为 JSON 序列化的工具 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.61</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Data Redis 默认使用 Jackson 作为 JSON 序列化的工具 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework</groupId>-->
|
||||
<!-- <artifactId>spring-tx</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework</groupId>-->
|
||||
<!-- <artifactId>spring-jdbc</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,8 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
//@EnableTransactionManagement
|
||||
public class Application {
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.cacheobject;
|
||||
|
||||
/**
|
||||
* 商品缓存对象
|
||||
*/
|
||||
public class ProductCacheObject {
|
||||
|
||||
/**
|
||||
* 产品编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 产品名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 产品分类编号
|
||||
*/
|
||||
private Integer cid;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ProductCacheObject setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ProductCacheObject setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getCid() {
|
||||
return cid;
|
||||
}
|
||||
|
||||
public ProductCacheObject setCid(Integer cid) {
|
||||
this.cid = cid;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ProductCacheObject{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", cid=" + cid +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.cacheobject;
|
||||
|
||||
/**
|
||||
* 用户缓存对象
|
||||
*/
|
||||
public class UserCacheObject {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserCacheObject setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UserCacheObject setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public UserCacheObject setGender(Integer gender) {
|
||||
this.gender = gender;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserCacheObject{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", gender=" + gender +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.config;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.listener.TestChannelTopicMessageListener;
|
||||
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.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisConfiguration {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
// 创建 RedisTemplate 对象
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
|
||||
// 设置开启事务支持
|
||||
template.setEnableTransactionSupport(true);
|
||||
|
||||
// 设置 RedisConnection 工厂。😈 它就是实现多种 Java Redis 客户端接入的秘密工厂。感兴趣的胖友,可以自己去撸下。
|
||||
template.setConnectionFactory(factory);
|
||||
|
||||
// 使用 String 序列化方式,序列化 KEY 。
|
||||
template.setKeySerializer(RedisSerializer.string());
|
||||
|
||||
// 使用 JSON 序列化方式(库是 Jackson ),序列化 VALUE 。
|
||||
template.setValueSerializer(RedisSerializer.json());
|
||||
return template;
|
||||
}
|
||||
|
||||
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
// ObjectMapper objectMapper = new ObjectMapper();// <1>
|
||||
//// objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
//// objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
//
|
||||
// jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
|
||||
// template.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
|
||||
// @Bean // PUB/SUB 使用的 Bean ,需要时打开。
|
||||
public RedisMessageListenerContainer listenerContainer(RedisConnectionFactory factory) {
|
||||
// 创建 RedisMessageListenerContainer 对象
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
|
||||
// 设置 RedisConnection 工厂。😈 它就是实现多种 Java Redis 客户端接入的秘密工厂。感兴趣的胖友,可以自己去撸下。
|
||||
container.setConnectionFactory(factory);
|
||||
|
||||
// 添加监听器
|
||||
container.addMessageListener(new TestChannelTopicMessageListener(), new ChannelTopic("TEST"));
|
||||
// container.addMessageListener(new TestChannelTopicMessageListener(), new ChannelTopic("AOTEMAN"));
|
||||
// container.addMessageListener(new TestPatternTopicMessageListener(), new PatternTopic("TEST"));
|
||||
return container;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 数据库访问层
|
||||
*/
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.dao;
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.dao.redis;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.cacheobject.UserCacheObject;
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.util.JSONUtil;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Repository
|
||||
public class UserCacheDao {
|
||||
|
||||
private static final String KEY_PATTERN = "user:%d"; // user:用户编号
|
||||
|
||||
@Resource(name = "redisTemplate")
|
||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||
private ValueOperations<String, String> operations;
|
||||
|
||||
private static String buildKey(Integer id) {
|
||||
return String.format(KEY_PATTERN, id);
|
||||
}
|
||||
|
||||
public UserCacheObject get(Integer id) {
|
||||
String key = buildKey(id);
|
||||
String value = operations.get(key);
|
||||
return JSONUtil.parseObject(value, UserCacheObject.class);
|
||||
}
|
||||
|
||||
public void set(Integer id, UserCacheObject object) {
|
||||
String key = buildKey(id);
|
||||
String value = JSONUtil.toJSONString(object);
|
||||
operations.set(key, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.listener;
|
||||
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
|
||||
public class TestChannelTopicMessageListener implements MessageListener {
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
System.out.println("收到 ChannelTopic 消息:");
|
||||
System.out.println("线程编号:" + Thread.currentThread().getName());
|
||||
System.out.println("message:" + message);
|
||||
System.out.println("pattern:" + new String(pattern));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.listener;
|
||||
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
|
||||
public class TestPatternTopicMessageListener implements MessageListener {
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
System.out.println("收到 PatternTopic 消息:");
|
||||
System.out.println("线程编号:" + Thread.currentThread().getName());
|
||||
System.out.println("message:" + message);
|
||||
System.out.println("pattern:" + new String(pattern));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService01 {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.service;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.cacheobject.UserCacheObject;
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.dao.redis.UserCacheDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService02 {
|
||||
|
||||
@Autowired
|
||||
private UserCacheDao userCacheDao;
|
||||
|
||||
public UserCacheObject get(Integer id) {
|
||||
return userCacheDao.get(id);
|
||||
}
|
||||
|
||||
public void set(Integer id, UserCacheObject object) {
|
||||
userCacheDao.set(id, object);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
/**
|
||||
* JSON 工具类
|
||||
*/
|
||||
public class JSONUtil {
|
||||
|
||||
public static <T> T parseObject(String text, Class<T> clazz) {
|
||||
return JSON.parseObject(text, clazz);
|
||||
}
|
||||
|
||||
public static String toJSONString(Object javaObject) {
|
||||
return JSON.toJSONString(javaObject);
|
||||
}
|
||||
|
||||
public static byte[] toJSONBytes(Object javaObject) {
|
||||
return JSON.toJSONBytes(javaObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
# 对应 RedisProperties 类
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
# password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码!
|
||||
database: 0 # Redis 数据库号,默认为 0 。
|
||||
timeout: 0 # Redis 连接超时时间,单位:毫秒。
|
||||
# 对应 RedissonProperties 类
|
||||
redisson:
|
||||
config: classpath:redisson.yaml # 具体的每个配置项,见 org.redisson.config.Config 类。
|
||||
@@ -0,0 +1,5 @@
|
||||
if redis.call('GET', KEYS[1]) ~= ARGV[1] then
|
||||
return 0
|
||||
end
|
||||
redis.call('SET', KEYS[1], ARGV[2])
|
||||
return 1
|
||||
@@ -0,0 +1,5 @@
|
||||
if redis.call('GET', KEYS[1]) != ARGV[1] then
|
||||
return {0}
|
||||
end
|
||||
redis.call('SET', KEYS[2], ARGV[2])
|
||||
return {1}
|
||||
@@ -0,0 +1 @@
|
||||
return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}
|
||||
@@ -0,0 +1,47 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.RedisCallback;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class PipelineTest {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Test
|
||||
public void test01() {
|
||||
List<Object> results = stringRedisTemplate.executePipelined(new RedisCallback<Object>() {
|
||||
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
// set 写入
|
||||
for (int i = 0; i < 3; i++) {
|
||||
connection.set(String.format("yunai:%d", i).getBytes(), "shuai".getBytes());
|
||||
}
|
||||
|
||||
// get
|
||||
for (int i = 0; i < 3; i++) {
|
||||
connection.get(String.format("yunai:%d", i).getBytes());
|
||||
}
|
||||
|
||||
// 返回 null 即可
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
// 打印结果
|
||||
System.out.println(results);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class PubSubTest {
|
||||
|
||||
public static final String TOPIC = "TEST";
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Test
|
||||
public void test01() throws InterruptedException {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
stringRedisTemplate.convertAndSend(TOPIC, "yunai:" + i);
|
||||
Thread.sleep(1000L);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||
import org.springframework.data.redis.core.script.RedisScript;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ScriptTest {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Test
|
||||
public void test01() throws IOException {
|
||||
// 读取 /resources/lua/compareAndSet.lua 脚本 。注意,需要引入下 commons-io 依赖。
|
||||
String scriptContents = IOUtils.toString(getClass().getResourceAsStream("/lua/compareAndSet.lua"), "UTF-8");
|
||||
// 创建 RedisScript 对象
|
||||
RedisScript<Long> script = new DefaultRedisScript<>(scriptContents, Long.class);
|
||||
// 执行 LUA 脚本
|
||||
Long result = stringRedisTemplate.execute(script, Collections.singletonList("yunai:1"), "shuai02", "shuai");
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test02() throws IOException {
|
||||
// 读取 /resources/lua/compareAndSet.lua 脚本 。注意,需要引入下 commons-io 依赖。
|
||||
String scriptContents = IOUtils.toString(getClass().getResourceAsStream("/lua/test.lua"), "UTF-8");
|
||||
// 创建 RedisScript 对象
|
||||
RedisScript<List> script = new DefaultRedisScript<>(scriptContents, List.class);
|
||||
|
||||
List<Object> result = stringRedisTemplate.execute(script, Arrays.asList("key1", "key2"), "first", "second");
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.SessionCallback;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SessionTest {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Test
|
||||
public void test01() {
|
||||
String result = stringRedisTemplate.execute(new SessionCallback<String>() {
|
||||
|
||||
@Override
|
||||
public String execute(RedisOperations operations) throws DataAccessException {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
operations.opsForValue().set(String.format("yunai:%d", i), "shuai02");
|
||||
}
|
||||
return (String) operations.opsForValue().get(String.format("yunai:%d", 0));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
System.out.println("result:" + result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.cacheobject.UserCacheObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.StringRedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class Test01 {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Test
|
||||
public void testStringSetKey() {
|
||||
stringRedisTemplate.opsForValue().set("yunai", "shuai");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringSetKey02() {
|
||||
redisTemplate.opsForValue().set("yunai", "shuai");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAdd() {
|
||||
stringRedisTemplate.opsForSet().add("yunai_descriptions", "shuai", "cai");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringSetKeyUserCache() {
|
||||
UserCacheObject object = new UserCacheObject()
|
||||
.setId(1)
|
||||
.setName("芋道源码")
|
||||
.setGender(1); // 男
|
||||
String key = String.format("user:%d", object.getId());
|
||||
redisTemplate.opsForValue().set(key, object);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringGetKeyUserCache() {
|
||||
String key = String.format("user:%d", 1);
|
||||
Object value = redisTemplate.opsForValue().get(key);
|
||||
System.out.println(value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
//@EnableTransactionManagement
|
||||
public class TransactionTest {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void test01() {
|
||||
// 这里是偷懒,没在 RedisConfiguration 配置类中,设置 stringRedisTemplate 开启事务。
|
||||
stringRedisTemplate.setEnableTransactionSupport(true);
|
||||
|
||||
// 执行想要的操作
|
||||
stringRedisTemplate.opsForValue().set("yunai:1", "shuai");
|
||||
stringRedisTemplate.opsForValue().set("yudaoyuanma:1", "dai");
|
||||
|
||||
// stringRedisTemplate.execute(new SessionCallback<String>() {
|
||||
//
|
||||
// @Override
|
||||
// public <K, V> String execute(RedisOperations<K, V> operations) throws DataAccessException {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
//// @Override
|
||||
//// public List<String> execute(RedisOperations<String, String> operations) throws DataAccessException {
|
||||
//// for (int i = 0; i < 100; i++) {
|
||||
//// operations.opsForValue(String.format("yunai:%d", i), "shuai");
|
||||
//// }
|
||||
//// return null;
|
||||
//// }
|
||||
//
|
||||
// });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test02() {
|
||||
stringRedisTemplate.setEnableTransactionSupport(true);
|
||||
|
||||
|
||||
stringRedisTemplate.opsForValue().get("user:1");
|
||||
stringRedisTemplate.opsForValue().get("user:2");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.cacheobject.UserCacheObject;
|
||||
import cn.iocoder.springboot.labs.lab10.springdatarediswithjedis.service.UserService02;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class UserService02Test {
|
||||
|
||||
@Autowired
|
||||
private UserService02 userService;
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
UserCacheObject object = new UserCacheObject()
|
||||
.setId(1)
|
||||
.setName("芋道源码")
|
||||
.setGender(1); // 男
|
||||
userService.set(object.getId(), object);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package cn.iocoder.springboot.labs.lab10.springdatarediswithjedis;
|
||||
Binary file not shown.
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
# 对应 RedisProperties 类
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
# password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码!
|
||||
database: 0 # Redis 数据库号,默认为 0 。
|
||||
timeout: 0 # Redis 连接超时时间,单位:毫秒。
|
||||
# 对应 RedissonProperties 类
|
||||
redisson:
|
||||
config: classpath:redisson.yaml
|
||||
@@ -0,0 +1,5 @@
|
||||
if redis.call('GET', KEYS[1]) ~= ARGV[1] then
|
||||
return 0
|
||||
end
|
||||
redis.call('SET', KEYS[1], ARGV[2])
|
||||
return 1
|
||||
@@ -0,0 +1 @@
|
||||
return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}
|
||||
Binary file not shown.
@@ -13,6 +13,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-07-spring-data-redis-with-jedis</module>
|
||||
<module>lab-07-spring-data-redis-with-redisson</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user