Skip to content

Commit

Permalink
spring data redis 测试
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Sep 28, 2019
1 parent a2aab39 commit a054aaf
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ public class RedisConfiguration {

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 创建 RedisTemplate 对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 设置 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);
// 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);
return template;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class UserCacheDao {

private static final String KEY_PATTERN = "user:%d"; // user:用户编号

// @Autowired
// private StringRedisTemplate template;
@Resource(name = "redisTemplate")
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
private ValueOperations<String, String> operations;
Expand All @@ -24,15 +22,13 @@ private static String buildKey(Integer id) {

public UserCacheObject get(Integer id) {
String key = buildKey(id);
// String value = template.opsForValue().get(key);
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);
// template.opsForValue().set(key, value);
operations.set(key, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest
public class Test01 {

@Autowired
Expand All @@ -24,6 +24,11 @@ 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");
Expand Down

0 comments on commit a054aaf

Please sign in to comment.