安装Redis可以参考 https://www.cnblogs.com/dddyyy/p/9763098.html
Redis的学习可以参考https://www.cnblogs.com/dddyyy/p/9803828.html 谢谢啦!
1.基本使用
因为Jedis工具类使用的是连接池,那么我们就来看一下连接池最简单的Demo,这样可以有助于了解后面的工具类代码。
先可以使用Maven工程来导包呀
<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 {
@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();
}
}
测试结果
2.工具类
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>