在之前的博客中,有提到过Redis 在服务端的一些相关知识,今天主要讲一下Java 整合Redis的相关内容。
下面是Jedis 的相关依赖:
redis.clients jedis 2.5.1 org.springframework.data spring-data-redis 1.0.2.RELEASE
首先我们了解一下,使用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(); }
我们先来看直观的运行结果:
我们先从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);
}
http://www.hxtcpp.com/2017/0303/227761.html
http://www.hxtcpp.com/2017/0303/227759.html
http://www.hxtcpp.com/2017/0303/227757.html
http://www.hxtcpp.com/2017/0303/227755.html
http://www.hxtcpp.com/2017/0303/227753.html
http://www.hxtcpp.com/2017/0303/227751.html
http://www.hxtcpp.com/2017/0303/227749.html
http://www.hxtcpp.com/2017/0303/227746.html
http://www.hxtcpp.com/2017/0303/227744.html
http://www.hxtcpp.com/2017/0303/227742.html
http://www.hxtcpp.com/2017/0303/227740.html
http://www.hxtcpp.com/2017/0303/227737.html
http://www.hxtcpp.com/2017/0303/227735.html
http://www.hxtcpp.com/2017/0303/227733.html
http://www.hxtcpp.com/2017/0303/227731.html
http://www.hxtcpp.com/2017/0303/227729.html
http://www.hxtcpp.com/2017/0303/227721.html
http://www.hxtcpp.com/2017/0303/227716.html
http://www.hxtcpp.com/2017/0303/227714.html
http://www.hxtcpp.com/2017/0303/227710.html
http://www.hxtcpp.com/2017/0303/227708.html
http://www.hxtcpp.com/2017/0303/227706.html
http://www.hxtcpp.com/2017/0303/227704.html
http://www.hxtcpp.com/2017/0303/227702.html
http://www.hxtcpp.com/2017/0303/227700.html
http://www.hxtcpp.com/2017/0303/227698.html
http://www.hxtcpp.com/2017/0303/227696.html
http://www.hxtcpp.com/2017/0303/227694.html
http://www.hxtcpp.com/2017/0303/227692.html
http://www.hxtcpp.com/2017/0303/227691.html
http://www.hxtcpp.com/2017/0303/227687.html
http://www.hxtcpp.com/2017/0303/227686.html
http://www.hxtcpp.com/2017/0303/227685.html
http://www.hxtcpp.com/2017/0303/227684.html
http://www.hxtcpp.com/2017/0303/227682.html
http://www.hxtcpp.com/2017/0303/227680.html
http://www.hxtcpp.com/2017/0303/227678.html
http://www.hxtcpp.com/2017/0303/227676.html
http://www.hxtcpp.com/2017/0303/227673.html
http://www.hxtcpp.com/2017/0303/227672.html
http://www.hxtcpp.com/2017/0303/227670.html
http://www.hxtcpp.com/2017/0303/227668.html
http://www.hxtcpp.com/2017/0303/227667.html
http://www.hxtcpp.com/2017/0303/227666.html
http://www.hxtcpp.com/2017/0303/227665.html
http://www.hxtcpp.com/2017/0303/227664.html
http://www.hxtcpp.com/2017/0303/227663.html
http://www.hxtcpp.com/2017/0303/227662.html
http://www.hxtcpp.com/2017/0303/227661.html
http://www.hxtcpp.com/2017/0303/227660.html
http://www.hxtcpp.com/2017/0303/227659.html
http://www.hxtcpp.com/2017/0303/227658.html
http://www.hxtcpp.com/2017/0303/227657.html
http://www.hxtcpp.com/2017/0303/227656.html
http://www.hxtcpp.com/2017/0303/227654.html
http://www.hxtcpp.com/2017/0303/227652.html
http://www.hxtcpp.com/2017/0303/227650.html
http://www.hxtcpp.com/2017/0303/227648.html
http://www.hxtcpp.com/2017/0303/227645.html
http://www.hxtcpp.com/2017/0303/227643.html
http://www.hxtcpp.com/2017/0303/227641.html
http://www.hxtcpp.com/2017/0303/227639.html
http://www.hxtcpp.com/2017/0303/227637.html
http://www.hxtcpp.com/2017/0303/227635.html
http://www.hxtcpp.com/2017/0303/227634.html
http://www.hxtcpp.com/2017/0303/227633.html
http://www.hxtcpp.com/2017/0303/227632.html
http://www.hxtcpp.com/2017/0303/227630.html
http://www.hxtcpp.com/2017/0303/227629.html
http://www.hxtcpp.com/2017/0303/227628.html
http://www.hxtcpp.com/2017/0303/227627.html
http://www.hxtcpp.com/2017/0303/227626.html
http://www.hxtcpp.com/2017/0303/227625.html
http://www.hxtcpp.com/2017/0303/227624.html
http://www.hxtcpp.com/2017/0303/227623.html
http://www.hxtcpp.com/2017/0303/227621.html
http://www.hxtcpp.com/2017/0303/227619.html
http://www.hxtcpp.com/2017/0303/227616.html
http://www.hxtcpp.com/2017/0303/227614.html
http://www.hxtcpp.com/2017/0303/227613.html
http://www.hxtcpp.com/2017/0303/227612.html
http://www.hxtcpp.com/2017/0303/227611.html
http://www.hxtcpp.com/2017/0303/227610.html
http://www.hxtcpp.com/2017/0303/227609.html
http://www.hxtcpp.com/2017/0303/227607.html
http://www.hxtcpp.com/2017/0303/227606.html
http://www.hxtcpp.com/2017/0303/227605.html
http://www.hxtcpp.com/2017/0303/227604.html
http://www.hxtcpp.com/2017/0303/227603.html
http://www.hxtcpp.com/2017/0303/227602.html
http://www.hxtcpp.com/2017/0303/227601.html
//根据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 进行操作。
在调用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();
http://www.hxtcpp.com/2017/0303/227600.html
http://www.hxtcpp.com/2017/0303/227595.html
http://www.hxtcpp.com/2017/0303/227589.html
http://www.hxtcpp.com/2017/0303/227588.html
http://www.hxtcpp.com/2017/0303/227587.html
http://www.hxtcpp.com/2017/0303/227586.html
http://www.hxtcpp.com/2017/0303/227585.html
http://www.hxtcpp.com/2017/0303/227584.html
http://www.hxtcpp.com/2017/0303/227583.html
http://www.hxtcpp.com/2017/0303/227582.html
http://www.hxtcpp.com/2017/0303/227581.html
http://www.hxtcpp.com/2017/0303/227580.html
http://www.hxtcpp.com/2017/0303/227579.html
http://www.hxtcpp.com/2017/0303/227574.html
http://www.hxtcpp.com/2017/0303/227562.html
http://www.hxtcpp.com/2017/0303/227550.html
http://www.hxtcpp.com/2017/0303/227542.html
http://www.hxtcpp.com/2017/0303/227541.html
http://www.hxtcpp.com/2017/0303/227540.html
http://www.hxtcpp.com/2017/0303/227539.html
http://www.hxtcpp.com/2017/0303/227538.html
http://www.hxtcpp.com/2017/0303/227537.html
http://www.hxtcpp.com/2017/0303/227536.html
http://www.hxtcpp.com/2017/0303/227535.html
http://www.hxtcpp.com/2017/0303/227534.html
http://www.hxtcpp.com/2017/0303/227533.html
http://www.hxtcpp.com/2017/0303/227522.html
http://www.hxtcpp.com/2017/0303/227520.html
http://www.hxtcpp.com/2017/0303/227516.html
http://www.hxtcpp.com/2017/0303/227514.html
http://www.hxtcpp.com/2017/0303/227512.html
http://www.hxtcpp.com/2017/0303/227510.html
http://www.hxtcpp.com/2017/0303/227508.html
http://www.hxtcpp.com/2017/0303/227506.html
http://www.hxtcpp.com/2017/0303/227504.html
http://www.hxtcpp.com/2017/0303/227502.html
http://www.hxtcpp.com/2017/0303/227500.html
http://www.hxtcpp.com/2017/0303/227498.html
http://www.hxtcpp.com/2017/0303/227497.html
http://www.hxtcpp.com/2017/0303/227496.html
http://www.hxtcpp.com/2017/0303/227495.html
http://www.hxtcpp.com/2017/0303/227491.html
http://www.hxtcpp.com/2017/0303/227489.html
http://www.hxtcpp.com/2017/0303/227487.html
http://www.hxtcpp.com/2017/0303/227484.html
http://www.hxtcpp.com/2017/0303/227482.html
http://www.hxtcpp.com/2017/0303/227480.html
http://www.hxtcpp.com/2017/0303/227478.html
http://www.hxtcpp.com/2017/0303/227476.html
http://www.hxtcpp.com/2017/0303/227473.html
http://www.hxtcpp.com/2017/0303/227468.html
http://www.hxtcpp.com/2017/0303/227466.html
http://www.hxtcpp.com/2017/0303/227464.html
http://www.hxtcpp.com/2017/0303/227462.html
http://www.hxtcpp.com/2017/0303/227460.html
http://www.hxtcpp.com/2017/0303/227458.html
http://www.hxtcpp.com/2017/0303/227456.html
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; } }
在Spring官网中,给出了这样得一个Demo:
http://www.hxtcpp.com/2017/0303/227454.html http://www.hxtcpp.com/2017/0303/227451.html http://www.hxtcpp.com/2017/0303/227449.html http://www.hxtcpp.com/2017/0303/227447.html http://www.hxtcpp.com/2017/0303/227445.html http://www.hxtcpp.com/2017/0303/227443.html http://www.hxtcpp.com/2017/0303/227441.html http://www.hxtcpp.com/2017/0303/227439.html http://www.hxtcpp.com/2017/0303/227435.html http://www.hxtcpp.com/2017/0303/227431.html http://www.hxtcpp.com/2017/0303/227429.html http://www.hxtcpp.com/2017/0303/227416.html http://www.hxtcpp.com/2017/0303/227414.html http://www.hxtcpp.com/2017/0303/227412.html http://www.hxtcpp.com/2017/0303/227410.html http://www.hxtcpp.com/2017/0303/227408.html http://www.hxtcpp.com/2017/0303/227406.html http://www.hxtcpp.com/2017/0303/227404.html http://www.hxtcpp.com/2017/0303/227402.html http://www.hxtcpp.com/2017/0303/227400.html http://www.hxtcpp.com/2017/0303/227398.html http://www.hxtcpp.com/2017/0303/227394.html http://www.hxtcpp.com/2017/0303/227389.html http://www.hxtcpp.com/2017/0303/227379.html http://www.hxtcpp.com/2017/0303/227377.html http://www.hxtcpp.com/2017/0303/227375.html http://www.hxtcpp.com/2017/0303/227373.html http://www.hxtcpp.com/2017/0303/227367.html http://www.hxtcpp.com/2017/0303/227365.html http://www.hxtcpp.com/2017/0303/227361.html http://www.hxtcpp.com/2017/0303/227359.html http://www.hxtcpp.com/2017/0302/227035.html http://www.hxtcpp.com/2017/0302/227033.html http://www.hxtcpp.com/2017/0302/227031.html http://www.hxtcpp.com/2017/0302/227029.html http://www.hxtcpp.com/2017/0302/227025.html http://www.hxtcpp.com/2017/0302/227023.html http://www.hxtcpp.com/2017/0302/227021.html http://www.hxtcpp.com/2017/0302/227019.html http://www.hxtcpp.com/2017/0302/227017.html http://www.hxtcpp.com/2017/0302/227015.html http://www.hxtcpp.com/2017/0302/227011.html http://www.hxtcpp.com/2017/0302/227007.html http://www.hxtcpp.com/2017/0302/227004.html http://www.hxtcpp.com/2017/0302/227002.html http://www.hxtcpp.com/2017/0302/226998.html http://www.hxtcpp.com/2017/0302/226997.html http://www.hxtcpp.com/2017/0302/226996.html http://www.hxtcpp.com/2017/0302/226928.html http://www.hxtcpp.com/2017/0302/226926.html http://www.hxtcpp.com/2017/0302/226924.html http://www.hxtcpp.com/2017/0302/226922.html http://www.hxtcpp.com/2017/0302/226911.html http://www.hxtcpp.com/2017/0302/226909.html http://www.hxtcpp.com/2017/0302/226906.html http://www.hxtcpp.com/2017/0302/226905.html http://www.hxtcpp.com/2017/0302/226904.html http://www.hxtcpp.com/2017/0302/226903.html http://www.hxtcpp.com/2017/0302/226900.html http://www.hxtcpp.com/2017/0302/226895.html http://www.hxtcpp.com/2017/0302/226890.html http://www.hxtcpp.com/2017/0302/226889.html http://www.hxtcpp.com/2017/0302/226886.html http://www.hxtcpp.com/2017/0302/226883.html http://www.hxtcpp.com/2017/0302/226880.html http://www.hxtcpp.com/2017/0302/226875.html http://www.hxtcpp.com/2017/0302/226873.html http://www.hxtcpp.com/2017/0302/226862.html http://www.hxtcpp.com/2017/0302/226857.html http://www.hxtcpp.com/2017/0302/226851.html
这个配置文件比较直接的帮我们配置了 jedisConectionFactory ,我们需要做的是注入这个Bean 之后,在工厂里面获取到Connection,然后进行相应的操作。
根据常用的场景,我们用到的比较多的是 Pool
接下来我们研究一下,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); }
http://www.hxtcpp.com/2017/0303/227357.html http://www.hxtcpp.com/2017/0303/227355.html http://www.hxtcpp.com/2017/0303/227348.html http://www.hxtcpp.com/2017/0303/227346.html http://www.hxtcpp.com/2017/0303/227344.html http://www.hxtcpp.com/2017/0303/227340.html http://www.hxtcpp.com/2017/0303/227336.html http://www.hxtcpp.com/2017/0303/227334.html http://www.hxtcpp.com/2017/0303/227332.html http://www.hxtcpp.com/2017/0303/227330.html http://www.hxtcpp.com/2017/0303/227328.html http://www.hxtcpp.com/2017/0303/227326.html http://www.hxtcpp.com/2017/0303/227324.html http://www.hxtcpp.com/2017/0303/227322.html http://www.hxtcpp.com/2017/0303/227319.html http://www.hxtcpp.com/2017/0303/227317.html http://www.hxtcpp.com/2017/0303/227315.html http://www.hxtcpp.com/2017/0303/227313.html http://www.hxtcpp.com/2017/0303/227311.html http://www.hxtcpp.com/2017/0303/227309.html http://www.hxtcpp.com/2017/0303/227307.html http://www.hxtcpp.com/2017/0303/227305.html http://www.hxtcpp.com/2017/0303/227303.html http://www.hxtcpp.com/2017/0303/227299.html http://www.hxtcpp.com/2017/0303/227291.html http://www.hxtcpp.com/2017/0303/227283.html http://www.hxtcpp.com/2017/0303/227281.html http://www.hxtcpp.com/2017/0303/227279.html http://www.hxtcpp.com/2017/0303/227277.html http://www.hxtcpp.com/2017/0303/227275.html http://www.hxtcpp.com/2017/0303/227273.html http://www.hxtcpp.com/2017/0302/227128.html http://www.hxtcpp.com/2017/0302/227124.html http://www.hxtcpp.com/2017/0302/227121.html http://www.hxtcpp.com/2017/0302/227117.html http://www.hxtcpp.com/2017/0302/227115.html http://www.hxtcpp.com/2017/0302/227113.html http://www.hxtcpp.com/2017/0302/227111.html http://www.hxtcpp.com/2017/0302/227109.html http://www.hxtcpp.com/2017/0302/227107.html http://www.hxtcpp.com/2017/0302/227105.html http://www.hxtcpp.com/2017/0302/227103.html http://www.hxtcpp.com/2017/0302/227101.html http://www.hxtcpp.com/2017/0302/227099.html http://www.hxtcpp.com/2017/0302/227097.html http://www.hxtcpp.com/2017/0302/227095.html http://www.hxtcpp.com/2017/0302/227093.html http://www.hxtcpp.com/2017/0302/227091.html http://www.hxtcpp.com/2017/0302/227089.html http://www.hxtcpp.com/2017/0302/227087.html http://www.hxtcpp.com/2017/0302/227085.html http://www.hxtcpp.com/2017/0302/227083.html http://www.hxtcpp.com/2017/0302/227081.html http://www.hxtcpp.com/2017/0302/227079.html http://www.hxtcpp.com/2017/0302/227077.html http://www.hxtcpp.com/2017/0302/227072.html http://www.hxtcpp.com/2017/0302/227070.html http://www.hxtcpp.com/2017/0302/227068.html http://www.hxtcpp.com/2017/0302/227066.html http://www.hxtcpp.com/2017/0302/227064.html http://www.hxtcpp.com/2017/0302/227062.html http://www.hxtcpp.com/2017/0302/227060.html http://www.hxtcpp.com/2017/0302/227058.html http://www.hxtcpp.com/2017/0302/227056.html http://www.hxtcpp.com/2017/0302/227054.html http://www.hxtcpp.com/2017/0302/227052.html http://www.hxtcpp.com/2017/0302/227048.html http://www.hxtcpp.com/2017/0302/227043.html http://www.hxtcpp.com/2017/0302/227041.html http://www.hxtcpp.com/2017/0302/227039.html http://www.hxtcpp.com/2017/0302/227037.htmlpublic 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 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'}