Redis的安裝和學習資料html
Redis的安裝能夠參考 http://www.javashuo.com/article/p-akjqwfxu-o.htmlnode
Redis的學習能夠參考http://www.javashuo.com/article/p-skknmiim-bu.html redis
由於Jedis工具類使用的是鏈接池,那麼咱們就來看一下鏈接池最簡單的Demo,這樣能夠有助於瞭解後面的工具類代碼。spring
先可使用Maven工程來導包須要的依賴包ide
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.3</version> <type>jar</type> <scope>compile</scope>
</dependency>
編寫個測試類
package com.JedisTest;工具
import org.junit.Test;學習
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;測試
public class JedisTest {htm
@Test
public void Test1() {
//建立一個鏈接池 參數爲你的Redis安裝的ip地址和端口
JedisPool jedisPool = new JedisPool("192.168.25.128",6379);
//從鏈接池拿個鏈接資源
Jedis jedis = jedisPool.getResource();//獲取資源
//設置Key-Value
jedis.set("myname", "dingyu");
//根據Key 取 Value
String name = jedis.get("myname");
System.out.println(name);
//關閉鏈接資源
jedis.close();
//關閉鏈接池
jedisPool.close();
}
}blog
測試結果
package JedisTool;
public interface JedisClient {
String set(String key, String value);
String get(String key);
Boolean exists(String key);
Long expire(String key, int seconds);
Long ttl(String key);
Long incr(String key);
Long hset(String key, String field, String value);
String hget(String key, String field);
Long hdel(String key,String... field);//刪除hkey
}
package JedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisClientPool implements JedisClient {
@Autowired
private JedisPool jedisPool;
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String result = jedis.set(key, value);
jedis.close();
return result;
}
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String result = jedis.get(key);
jedis.close();
return result;
}
@Override
public Boolean exists(String key) {
Jedis jedis = jedisPool.getResource();
Boolean result = jedis.exists(key);
jedis.close();
return result;
}
@Override
public Long expire(String key, int seconds) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, seconds);
jedis.close();
return result;
}
@Override
public Long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
@Override
public Long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public Long hset(String key, String field, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(key, field, value);
jedis.close();
return result;
}
@Override
public String hget(String key, String field) {
Jedis jedis = jedisPool.getResource();
String result = jedis.hget(key, field);
jedis.close();
return result;
}
@Override
public Long hdel(String key, String... field) {
Jedis jedis = jedisPool.getResource();
Long hdel = jedis.hdel(key, field);
jedis.close();
return hdel;
}
}
package JedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisCluster;
/**
* 主要用於集羣的時候鏈接Redis
*
*
*/
public class JedisClientCluster implements JedisClient {
@Autowired
private JedisCluster jedisCluster;
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public Boolean exists(String key) {
return jedisCluster.exists(key);
}
@Override
public Long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
}
@Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
}
@Override
public Long incr(String key) {
return jedisCluster.incr(key);
}
@Override
public Long hset(String key, String field, String value) {
return jedisCluster.hset(key, field, value);
}
@Override
public String hget(String key, String field) {
return jedisCluster.hget(key, field);
}
@Override
public Long hdel(String key, String... field) {
// TODO Auto-generated method stub
return jedisCluster.hdel(key, field);
}
}
集成配置文件
<!-- spring的寫法 -->
<!-- 配置單機版 -->
<bean class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.25.153"</constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<!-- 配置集羣版 -->
<bean class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
</set>
</constructor-arg></bean>