深入浅出Redis-Spring整合Redis
概述:
在之前的博客中,有提到过Redis 在服务端的一些相关知识,今天主要讲一下Java 整合Redis的相关内容。
下面是Jedis 的相关依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.1</version>
</dependency> <!-- redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
1、Jedis 单机客户端
首先我们了解一下,使用Jedis 连接我们的Redis 服务器:
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 6379;
Jedis jedis = new Jedis(host,port,50000);
jedis.set("name","jayce");
String name = jedis.get("name");
System.out.println(name);
jedis.close();
}
我们先来看直观的运行结果:
1.1 Jedis 构造函数:
我们先从Jedis 的构造函数开始说起,在Jedis的源码中,我们可以看到Jedis 提供以下几种构造函数:
从上图中我们可以看到一个很重要的信息,Jedis 继承BinaryJedis,并且它的所有构造函数都采用了父类的构造函数:
//根据host 默认端口6379获取连接
public Jedis(final String host) {
super(host);
}
//根据host 与特定端口获取连接
public Jedis(final String host, final int port) {
super(host, port);
}
//根据host 与特定端口获取连接,并且设定key的生命周期
public Jedis(final String host, final int port, final int timeout) {
super(host, port, timeout);
}
//根据JedisShardInfo 配置信息,获取连接
public Jedis(JedisShardInfo shardInfo) {
super(shardInfo);
}
//根据特定URI 获取连接
public Jedis(URI uri) {
super(uri);
}
我们可以观察到父类BinaryJedis 的构造函数中,最终的目的是为了创建一个client,而这个client实质上是一个connection:
因此,我们在创建一个Jedis 对象的过程中,创建了一个对服务器进行连接的Client,而接下来的相关操作,也是由这个client 进行操作。
1.2 Jedis 的Get和Set:
在调用Get和Set方法之前,我们需要创建一个连接,然后进行相应的操作,下述代码提供了设置字符串,和设置对象到Redis 中,需要注意的是,我们在将对象放到Redis 之前,需要将对象进行序列化,因此对象需要实现Serializable接口:
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 6381;
Jedis jedis = new Jedis(host, port);
setString(jedis);
setObject(jedis);
jedis.close();
} private static void setString(Jedis jedis) {
jedis.set("name", "jayce");
String name = jedis.get("name");
System.out.println(name);
} private static void setObject(Jedis jedis) {
User user = new User();
user.setId(1);
user.setName("jayce");
user.setPassword("kong");
byte[] values = SerializeUtil.serialize(user);
byte[] names = "user".getBytes();
jedis.set(names, values);
byte[] bytes = jedis.get("user".getBytes());
User userCache = (User) SerializeUtil.unserialize(bytes);
System.out.println(userCache); }
我们在服务器中的Redis 中可以看到:
数据已经缓存到了Redis 中。
然后,我们再跟踪一下,Jedis 是如何将一个对象存到了服务器中的:
第一步:Jedis 调用 set 方法,然后调用内部的client进行操作:
public String set(final String key, String value) {
checkIsInMulti();
client.set(key, value);
return client.getStatusCodeReply();
}
第二步:client 调用 SafeEncoder.encode(key) 方法,将字符串转换成二进制数组,再调用client 中的 set(byte[],byte[])方法:
public void set(final String key, final String value) {
set(SafeEncoder.encode(key), SafeEncoder.encode(value));
} public void set(final byte[] key, final byte[] value) {
sendCommand(Command.SET, key, value);
}
第三步:在调用父类Connection中的 sendCommand() 方法,最终将内容传到服务器中:
protected Connection sendCommand(final Command cmd, final byte[]... args) {
try {
connect();
Protocol.sendCommand(outputStream, cmd, args);
pipelinedCommands++;
return this;
} catch (JedisConnectionException ex) {
// Any other exceptions related to connection?
broken = true;
throw ex;
}
}
2、Jedis 单机版整合Spring
在Spring官网中,给出了这样得一个Demo:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="server" p:port="6379" /> </beans>
这个配置文件比较直接的帮我们配置了 jedisConectionFactory ,我们需要做的是注入这个Bean 之后,在工厂里面获取到Connection,然后进行相应的操作。
根据常用的场景,我们用到的比较多的是 Pool<Jedis>,因此,这里为大家分享的是JedisPool 的相关配置:
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="${redis.host}"/>
<constructor-arg name="port" value="${redis.port}"/>
<!--<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>-->
</bean>
接下来我们研究一下,JedisPool 的源码,方便我们对配置文件的理解:
public JedisPool(GenericObjectPoolConfig poolConfig, String host) {
this(poolConfig, host, 6379, 2000, (String)null, 0, (String)null);
} public JedisPool(String host, int port) {
this(new GenericObjectPoolConfig(), host, port, 2000, (String)null, 0, (String)null);
} public JedisPool(String host) {
URI uri = URI.create(host);
if(uri.getScheme() != null && uri.getScheme().equals("redis")) {
String h = uri.getHost();
int port = uri.getPort();
String password = uri.getUserInfo().split(":", 2)[1];
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
this.internalPool = new GenericObjectPool(new JedisFactory(h, port, 2000, password, database, (String)null), new GenericObjectPoolConfig());
} else {
this.internalPool = new GenericObjectPool(new JedisFactory(host, 6379, 2000, (String)null, 0, (String)null), new GenericObjectPoolConfig());
} } public JedisPool(URI uri) {
String h = uri.getHost();
int port = uri.getPort();
String password = uri.getUserInfo().split(":", 2)[1];
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
this.internalPool = new GenericObjectPool(new JedisFactory(h, port, 2000, password, database, (String)null), new GenericObjectPoolConfig());
} public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {
this(poolConfig, host, port, timeout, password, 0, (String)null);
} public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port) {
this(poolConfig, host, port, 2000, (String)null, 0, (String)null);
} public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout) {
this(poolConfig, host, port, timeout, (String)null, 0, (String)null);
} public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password, int database) {
this(poolConfig, host, port, timeout, password, database, (String)null);
} public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password, int database, String clientName) {
super(poolConfig, new JedisFactory(host, port, timeout, password, database, clientName));
}
JedisPool 的构造函数与上述的Jedis 的构造函数很相似,这里比较特别的是 GenericObjectPoolConfig 这个配置类,这个类 是org.apache.commons.pool2 包下面的一个用来设置池的大小的类
我们在配置application.xml文件的时候,可以自己配置对应的池大小,但是如果没有相应的配置文件的同学,推荐还是使用默认配置。
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-Redis.xml");
Pool<Jedis> jedisPool = (Pool)applicationContext.getBean("redisClient");
Jedis jedis = jedisPool.getResource();
setString(jedis);
setObject(jedis);
jedisPool.returnResource(jedis);
} private static void setString(Jedis jedis) {
jedis.set("name", "jayce");
String name = jedis.get("name");
System.out.println(name);
} private static void setObject(Jedis jedis) {
User user = new User();
user.setId(1);
user.setName("jayce");
user.setPassword("kong");
byte[] values = SerializeUtil.serialize(user);
byte[] names = "user".getBytes();
jedis.set(names, values);
byte[] bytes = jedis.get("user".getBytes());
User userCache = (User) SerializeUtil.unserialize(bytes);
System.out.println(userCache); }
运行结果:
jayce
User{id=1, name='jayce', password='kong'}
3、总结
项目源码:https://github.com/jaycekon/Crawl-Page
后面会继续总结,Spring 整合 Redis 集群~
深入浅出Redis-Spring整合Redis的更多相关文章
- 网站性能优化小结和spring整合redis
现在越来越多的地方需要非关系型数据库了,最近网站优化,当然从页面到服务器做了相应的优化后,通过在线网站测试工具与之前没优化对比,发现有显著提升. 服务器优化目前主要优化tomcat,在tomcat目录 ...
- Spring整合Redis&JSON序列化&Spring/Web项目部署相关
几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...
- spring整合redis之hello
1.pom.xml文件 <dependencies> <!-- spring核心包 --> <dependency> <groupId>org.spri ...
- Spring整合Redis时报错:java.util.NoSuchElementException: Unable to validate object
我在Spring整合Redis时报错,我是犯了一个很低级的错误! 我设置了Redis的访问密码,在Spring的配置文件却没有配置密码这一项,配置上密码后,终于不报错了!
- Redis的安装以及spring整合Redis时出现Could not get a resource from the pool
Redis的下载与安装 在Linux上使用wget http://download.redis.io/releases/redis-5.0.0.tar.gz下载源码到指定位置 解压:tar -xvf ...
- Spring整合redis实现key过期事件监听
打开redis服务的配置文件 添加notify-keyspace-events Ex 如果是注释了,就取消注释 这个是在以下基础上进行添加的 Spring整合redis:https://www. ...
- (转)Spring整合Redis作为缓存
采用Redis作为Web系统的缓存.用Spring的Cache整合Redis. 一.关于redis的相关xml文件的写法 <?xml version="1.0" ...
- spring整合redis客户端及缓存接口设计(转)
一.写在前面 缓存作为系统性能优化的一大杀手锏,几乎在每个系统或多或少的用到缓存.有的使用本地内存作为缓存,有的使用本地硬盘作为缓存,有的使用缓存服务器.但是无论使用哪种缓存,接口中的方法都是差不多. ...
- spring整合redis使用RedisTemplate的坑Could not get a resource from the pool
一.背景 项目中使用spring框架整合redis,使用框架封装的RedisTemplate来实现数据的增删改查,项目上线后,我发现运行一段时间后,会出现异常Could not get a resou ...
- spring整合redis(哨兵模式)
首先服务器搭建哨兵模式(我这里使用的是Windows8系统),感谢两位博主,少走不少弯路,在此给出链接:服务器哨兵模式搭建和整合哨兵模式 什么一些介绍就不介绍了,可以看一下连接,比较详细,初次接触,当 ...
随机推荐
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- 11.TCP的交互数据流
TCP报文段一般有两类,分别是成块数据和交互数据. 1.交互式输入 Rlogin连接上键入一个交互命令的数据流如下图所示. 每一个交互按键都会产生一个数据分组,每次从客户传 ...
- console 高级用法
console.log()用法,相信大家都很熟悉了,这里就不再啰嗦. 下面来玩几个新鲜点的,我用的是chrome28,不保证兼容其他浏览器:console.log的第一个参数中可以指定一个格式字符,这 ...
- IOS 动画的两种方式
方式一: [UIView animateWithDuration:1 animations:^{ //动画的内容 CGRect frame = CGRectMake([UIParam widthScr ...
- 淘宝的数据库拆分(TDDL)(转)
淘宝的数据拆分历程 系 统刚开始的时候,因为系统刚上线,用户不多,那个时候,所有的数据都放在了同一个数据库中,这个时候因为用户少压力小,一个数据库完全可以应付的了,但是 随着运营那些哥们辛苦的呐喊和拼 ...
- JavaScript 模块化及 SeaJs 源码分析
网页的结构越来越复杂,简直可以看做一个简单APP,如果还像以前那样把所有的代码都放到一个文件里面会有一些问题: 全局变量互相影响 JavaScript文件变大,影响加载速度 结构混乱.很难维护 和后端 ...
- eclipse调试找不到源解决办法
eclipse调试时有时显示找不到源码,首先得确定代码没问题 这是eclipse没有发现工程源码,解决办法是 右键工程>>Debug As >> Debug configura ...
- Flex中操作XML的E4X方法
用于处理 XML 的 E4X 方法 Flash Player 9 和更高版本,Adobe AIR 1.0 和更高版本 ECMAScript for XML 规范定义了一组用于使用 XML 数据的类 ...
- MySQL锁详解
一.概述 数据库锁定机制简单来说就是数据库为了保证数据的一致性而使各种共享资源在被并发访问访问变得有序所设计的一种规则.对于任何一种数据库来说都需要有相应的锁定机制,所以MySQL自然也不能例外.My ...
- CentOS7系统安装DNS服务
CentOS7系统安装DNS服务 30.1.DNS是什么? DNS ( Domain Name System )是"域名系统"的英文缩写,简单来说就是一个数据库,用于存储网络中IP ...