redis最佳实践
总结:
String类型的value(string/list/set/hash)使用StringRedisTemplate
其他类型的value(string/list/set/hash/object)使用RedisTemplate(GenericFastJsonRedisSerializer,value都会序列化为json)
value为string时,
如果使用RedisTemplate(使用GenericFastJsonRedisSerializer序列化)时,value值 带双引号。即:"遥远2"。
如果使用StringRedisTemplate,value值不带双引号。即:遥远2
1、pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
</dependencies>
2、application-dev.yml
spring:
redis:
host: 10.134.253.30
port: 6379
password: 123456
# 连接超时时间:ms
timeout: 5000
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
3、RedisConfig.java
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; import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; @Configuration
public class RedisConfig { @Bean("jsonRedisTemplate")
public RedisTemplate<Object,Object> jsonRedisTemplate(RedisConnectionFactory redisConectionFactory) {
RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
template.setConnectionFactory(redisConectionFactory); template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericFastJsonRedisSerializer());
//hash
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
4、测试类
4.1、StringRedisTemplateTest
import java.util.List; import lombok.extern.slf4j.Slf4j; 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.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class StringRedisTemplateTest { @Autowired
private StringRedisTemplate stringRedisTemplate; @Test
public void testValue() throws Exception {
stringRedisTemplate.opsForValue().set("zuo:1", "遥远2");
String val = stringRedisTemplate.opsForValue().get("zuo:1");
log.info("值={}",val);
}
@Test
public void testList() throws Exception {
stringRedisTemplate.opsForList().leftPush("list:zuo", "左");
stringRedisTemplate.opsForList().leftPush("list:zuo", "杨");
stringRedisTemplate.opsForList().leftPush("list:zuo", "王"); long len = stringRedisTemplate.opsForList().size("list:zuo");
for (int i = 0; i < len; i++) {
String val = stringRedisTemplate.opsForList().leftPop("list:zuo");//先进后出
/*
* 0:王
* 1:杨
* 2:左
*/
log.info("{}:{}",i,val);
}
}
@Test
public void testHash() throws Exception {
stringRedisTemplate.opsForHash().put("hash:zuo", "name", "遥远2");;
stringRedisTemplate.opsForHash().put("hash:zuo", "age", "18");
String age = (String)stringRedisTemplate.opsForHash().get("hash:zuo", "age");
log.info("{}",age);
}
@Test
public void testSet() throws Exception {
stringRedisTemplate.opsForSet().add("set:zuo", "1");
stringRedisTemplate.opsForSet().add("set:zuo", "2");
stringRedisTemplate.opsForSet().add("set:zuo", "3");
long len = stringRedisTemplate.opsForSet().size("set:zuo");
for (int i = 0; i < len; i++) {
String s = stringRedisTemplate.opsForSet().pop("set:zuo");
log.info("{}",s);
}
}
@Test
public void testBound() throws Exception {
BoundListOperations operations = stringRedisTemplate.boundListOps("list:zuo");
operations.leftPush("左");
operations.leftPush("杨");
operations.leftPush("王"); List<String> list = operations.range(0, operations.size());
list.stream().forEach(s -> System.out.println(s));
} }
4.2、RedisTemplateTest
import lombok.extern.slf4j.Slf4j; 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.test.context.junit4.SpringRunner; import com.ebc.entity.User;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class RedisTemplateTest {
@Autowired
private RedisTemplate<Object,Object> jsonRedisTemplate;//不能为RedisTemplate<K,V>
@Test
public void testHash() throws Exception {
String key = "redistemplate:hash";
jsonRedisTemplate.opsForHash().put(key, "name", "遥远2");
jsonRedisTemplate.opsForHash().put(key, "age", 18);
int age = (Integer)jsonRedisTemplate.opsForHash().get(key, "age");
log.info("{}", age);
}
@Test
public void testObj() throws Exception {
String key = "redistemplate:user";
jsonRedisTemplate.delete(key);
jsonRedisTemplate.opsForValue().set(key, User.getSampleUser()); User user = (User)jsonRedisTemplate.opsForValue().get(key);
log.info("{}", user);
} }
4.3、RedisTemplateTest2
import java.util.List; import lombok.extern.slf4j.Slf4j; 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.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class RedisTemplateTest2 { @Autowired()
private RedisTemplate<Object,Object> jsonRedisTemplate; @Test
public void testValue() throws Exception {
jsonRedisTemplate.opsForValue().set("redistemplate:value:int", 1);
jsonRedisTemplate.opsForValue().set("redistemplate:value:long", 1L);
jsonRedisTemplate.opsForValue().set("redistemplate:value:double", 1d); int i = (int)jsonRedisTemplate.opsForValue().get("redistemplate:value:int");
long l = (long)jsonRedisTemplate.opsForValue().get("redistemplate:value:long");
double d = (double)jsonRedisTemplate.opsForValue().get("redistemplate:value:double"); log.info("{},{},{}",i,l,d);
}
@Test
public void testList() throws Exception {
String key = "redistemplate:list";
jsonRedisTemplate.opsForList().leftPush(key, 1);
jsonRedisTemplate.opsForList().leftPush(key, 2);
jsonRedisTemplate.opsForList().leftPush(key, 3); long len = jsonRedisTemplate.opsForList().size(key);
for (int i = 0; i < len; i++) {
int val = (int)jsonRedisTemplate.opsForList().leftPop(key);//先进后出
/*
* 0:3
* 1:2
* 2:1
*/
log.info("{}:{}",i,val);
}
}
@Test
public void testSet() throws Exception {
String key = "redistemplate:set";
jsonRedisTemplate.opsForSet().add(key, 1);
jsonRedisTemplate.opsForSet().add(key, 2);
jsonRedisTemplate.opsForSet().add(key, 3);
long len = jsonRedisTemplate.opsForSet().size(key);
for (int i = 0; i < len; i++) {
int s = (int)jsonRedisTemplate.opsForSet().pop(key);
log.info("{}",s);
}
}
@Test
public void testBound() throws Exception {
BoundListOperations operations = jsonRedisTemplate.boundListOps("redistemplate:list");
operations.leftPush(1);
operations.leftPush(2);
operations.leftPush(3); List<Integer> list = operations.range(0, operations.size());
list.stream().forEach(s -> System.out.println(s));
} }
redis最佳实践的更多相关文章
- Spring Boot 2.x 整合 Redis最佳实践
一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...
- PHP核心技术与最佳实践——全局浏览
难得买到并喜欢一本好书,‘PHP核心技术与最佳实践’. 几天时间,先看了个大概,总结一下整体是什么样子的,怎么看怎么学. 1.总共14章: 2.第1.2章讲PHP的OOP: 其中第一章侧重于PHP的O ...
- 《开源安全运维平台OSSIM最佳实践》
<开源安全运维平台OSSIM最佳实践> 经多年潜心研究开源技术,历时三年创作的<开源安全运维平台OSSIM最佳实践>一书即将出版.该书用80多万字记录了,作者10多年的IT行业 ...
- mongodb 最佳实践
MongoDB功能预览:http://pan.baidu.com/s/1k2UfW MongoDB在赶集网的应用:http://pan.baidu.com/s/1bngxgLp MongoDB在京东的 ...
- celery最佳实践
作为一个Celery使用重度用户.看到Celery Best Practices这篇文章.不由得菊花一紧. 干脆翻译出来,同一时候也会添加我们项目中celery的实战经验. 至于Celery为何物,看 ...
- React服务器渲染最佳实践
源码地址:https://github.com/skyFi/dva-starter React服务器渲染最佳实践 dva-starter 完美使用 dva react react-router,最好用 ...
- Session 的原理及最佳实践
Http协议是基于请求和响应的一种无状态的协议,而通过session可以使得Http应用变得有状态,即可以"记住"客户端的信息.今天就来说说这个session和cookie. Se ...
- MySQL面试必考知识点:揭秘亿级高并发数据库调优与最佳实践法则
做业务,要懂基本的SQL语句: 做性能优化,要懂索引,懂引擎: 做分库分表,要懂主从,懂读写分离... 数据库的使用,是开发人员的基本功,对它掌握越清晰越深入,你能做的事情就越多. 今天我们用10分钟 ...
- Redis进阶实践之七Redis和Lua初步整合使用(转载 7)
Redis进阶实践之七Redis和Lua初步整合使用 一.引言 Redis学了一段时间了,基本的东西都没问题了.从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运 ...
随机推荐
- linux下MySQL5.6安装记录
MySQL下载地址: ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.6/http://mirrors.sohu.com/mysql/ ...
- 利用Python+阿里云实现DDNS(动态域名解析)
引子我想大家应该都很熟悉DNS了,这回在DNS前面加了一个D又变成了什么呢?这个D就是Dynamic(动态),也就是说,按照传统,一个域名所对应的IP地址应该是定死的,而使用了DDNS后,域名所对应的 ...
- 分享知识-快乐自己:Hibernate框架常用API详解
1):Configuration配置对象 Configuration用于加载配置文件. 1): 调用configure()方法,加载src下的hibernate.cfg.xml文件 Configura ...
- JDK中主要的包介绍
- js跨域请求方式 ---- JSONP原理解析
这篇文章主要介绍了js跨域请求的5中解决方式的相关资料,需要的朋友可以参考下 跨域请求数据解决方案主要有如下解决方法: 1 2 3 4 5 JSONP方式 表单POST方式 服务器代理 H ...
- unicode和utf-8互转
1.1 ASCII码 我们知道, 在计算机内部, 所有的信息最终都表示为一个二进制的字符串. 每一个二进制位(bit)有0和1两种状态, 因此八个二进制位就可以组合出 256种状态, 这被称为一个字节 ...
- BZOJ1798:[AHOI2009]维护序列
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...
- tyvj1015公路乘车——DP
题目:http://www.joyoi.cn/problem/tyvj-1015 代码如下: #include<iostream> #include<cstdio> using ...
- 分布式一致性协议之:Paxos算法(转)
Paxos算法的难理解与算法的知名度一样令人敬仰,从我个人的经历而言,难理解的原因并不是该算法高深到大家智商不够,而在于Lamport在表达该算法时过于晦涩且缺乏一个完整的应用场景.如果大师能换种思路 ...
- spring boot 学习三:OAuth2 认证
1: 代码地址: https://github.com/liufeiSAP/uaa-zuul 2: 安装: postgres 下载 https://www.openscg.com/bigsq ...