版权声明:作者原创,转载请注明出处。简介 redis是一个高性能的key-value内存数据库,通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型,在如今系统不断追求高并发、高效率的大环境下,redis被广泛使用。
本系列文章目录地址:http://blog.csdn.net/u011961421/article/details/79416510
实战 redis官方版本为Linux版本,在官网下载解压即可,windows下安装redis请参考(http://blog.csdn.net/u011961421/article/details/78862249)。在实际工作中,申请到redis机器后,同样可以按以下方式配置,redis集群这里暂不拓展。
在前工程基础上(即前四个学习目录,工程git地址为https://github.com/15651037763/cms),SpringBoot本身对redis做了扩展,集成redis要简便的多。
1.pom.xml引入redis包
org.springframework.boot
spring-boot-starter-redis
spring-boot-starter-redis也是对jedis做了封装。
2.配置redis
两种方式配置redis,第一种,在application.yml中加入redis配置:
spring:
redis:
database: 2
host: 127.0.0.1
port: 6379
#password
timeout: 5000
pool:
# 最大空闲连接数
max-idle: 8
# 最小空闲连接数
min-idle: 0
# 最大活跃连接数
max-active: 8
# 获取连接时的最大等待毫秒数(-1阻塞不确定的时间)
max-wait: -1
【Spring|SpringBoot学习(五)--SpringBoot集成redis缓存及redis客户端】第二种方式可以通过@Configuration编写配置类:
@Configuration
@EnableAutoConfiguration
public class RedisConfiguration {
private static final Logger log = LoggerFactory.getLogger(RedisConfiguration.class);
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig pool = new JedisPoolConfig();
pool.setMaxIdle(8);
pool.setMinIdle(0);
pool.setMaxTotal(8);
pool.setMaxWaitMillis(-1);
log.info("JedisPoolConfig bean init success.");
return pool;
}@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setPoolConfig(jedisPoolConfig());
connectionFactory.setHostName("127.0.0.1");
connectionFactory.setDatabase(2);
connectionFactory.setPort(6379);
connectionFactory.setTimeout(5000);
log.info("JedisConnectionFactory bean init success.");
return connectionFactory;
}@Bean
public RedisTemplate redisTemplate() {
log.info("RedisTemplate bean init success.");
return new StringRedisTemplate(connectionFactory());
}
}
未配置redis的情况下,jedis会查找默认本地和默认端口的连接,默认配置可以在Connection.class看到,如下:
public class Connection implements Closeable {
private static final byte[][] EMPTY_ARGS = new byte[0][];
private String host = "localhost";
private int port = 6379;
private Socket socket;
private RedisOutputStream outputStream;
private RedisInputStream inputStream;
private int pipelinedCommands = 0;
private int connectionTimeout = 2000;
private int soTimeout = 2000;
private boolean broken = false;
private boolean ssl;
private SSLSocketFactory sslSocketFactory;
private SSLParameters sslParameters;
private HostnameVerifier hostnameVerifier;
public Connection() {
}public Connection(String host) {
this.host = host;
}
...
...
3.封装StringRedisTemplate
StringRedisTemplate是Spring继承RedisTemplate基础上封装的操作模板,两者之间的区别可以参考(http://blog.csdn.net/notsaltedfish/article/details/75948281),为了方便自己使用,再做一次封装,也有利于了解StringRedisTemplate的API。
public class RedisManagerImpl implements RedisManager {
@Autowired
private StringRedisTemplate redisTemp;
@Override
public String getStr(String key) {
return redisTemp.opsForValue().get(key);
}@Override
public String getSubStr(String key, long start, long end) {
return redisTemp.opsForValue().get(key, start, end);
}@Override
public void setStr(String key, String value) {
redisTemp.opsForValue().set(key, value);
}@Override
public void setStrAndExpire(String key, String value, long timeout) {
redisTemp.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}@Override
public void setStrAndCover(String key, String value, long offset) {
redisTemp.opsForValue().set(key, value, offset);
}@Override
public boolean setStrIfAbsent(String key, String value) {
return redisTemp.opsForValue().setIfAbsent(key, value);
}@Override
public void multiSetStr(Map params) {
redisTemp.opsForValue().multiSet(params);
}@Override
public boolean multiSetStrIfAbsent(Map params) {
return redisTemp.opsForValue().multiSetIfAbsent(params);
}@Override
public long getExpire(String key) {
return redisTemp.getExpire(key);
}@Override
public boolean setExpire(String key, long timeout) {
return redisTemp.expire(key, timeout, TimeUnit.SECONDS);
}@Override
public boolean hasKey(String key) {
return redisTemp.hasKey(key);
}@Override
public void delete(String key) {
redisTemp.delete(key);
}@Override
public void multiDelete(List keys) {
redisTemp.delete(keys);
}@Override
public boolean renameKeyIfAbsent(String oldKey, String newKey) {
return redisTemp.renameIfAbsent(oldKey, newKey);
}@Override
public boolean hasHashKey(String key, Object hashKey) {
return redisTemp.opsForHash().hasKey(key, hashKey);
}@Override
public Object hashGet(String key, Object hashKey) {
return redisTemp.opsForHash().get(key, hashKey);
}@Override
public Map getHashMap(String key) {
return redisTemp.opsForHash().entries(key);
}@Override
public long hashDelete(String key, Object... hashKeys) {
return redisTemp.opsForHash().delete(key, hashKeys);
}@Override
public void hashPut(String key, Object hashKey, Object hashValue) {
redisTemp.opsForHash().put(key, hashKey, hashValue);
}@Override
public boolean hashPutIfAbsent(String key, Object hashKey, Object hashValue) {
return redisTemp.opsForHash().putIfAbsent(key, hashKey, hashValue);
}@Override
public void setHashMap(String key, HashMap map) {
redisTemp.opsForHash().putAll(key, map);
}@Override
public List getList(String key) {
return redisTemp.opsForList().range(key, 0, -1);
}@Override
public void leftListPush(String key, String... values) {
redisTemp.opsForList().leftPushAll(key, values);
}@Override
public void leftSetList(String key, List value) {
redisTemp.opsForList().leftPushAll(key, value);
}@Override
public void rightListPush(String key, String... values) {
redisTemp.opsForList().rightPushAll(key, values);
}@Override
public void rightSetList(String key, List value) {
redisTemp.opsForList().rightPushAll(key, value);
}@Override
public String leftListPop(String key) {
return redisTemp.opsForList().leftPop(key);
}@Override
public String rightListPop(String key) {
return redisTemp.opsForList().rightPop(key);
}@Override
public void ListRemove(String key, long count, Object value) {
redisTemp.opsForList().remove(key, count, value);
}
}
4.测试,编写Demo方法,验证redis,如下图
@RequestMapping(method = RequestMethod.GET, value = "https://www.it610.com/testRedis")
@ResponseBody
public String testRedis(String key) {
System.out.println("入参key为:"+key);
String s = "";
if(!StringUtils.isEmpty(key)) {
s = "查询结果为:"+redisManager.getStr(key);
}
return s;
}
文章图片
文章图片
推荐阅读
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- jar|springboot项目打成jar包和war包,并部署(快速打包部署)
- 数据库|效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- java人生|35K 入职华为Java开发那天,我哭了(这 5 个月做的一切都值了)
- Redis|redis原理之布隆过滤器(Bloom Filter)
- Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)
- 微服务|微服务系列:服务发现与注册-----Eureka(面试突击!你想了解的Eureka都在这里.持续更新中......)
- redis安装与基本使用
- java|ApplicationListener和SpringApplicationRunListener的联系
- Spring|SpringSecurity--自定义登录页面、注销登录配置