Jedis工具类代码
程序员文章站
2022-07-02 15:49:56
安装Redis可以参考 https://www.cnblogs.com/dddyyy/p/9763098.html Redis的学习可以参考https://www.cnblogs.com/dddyyy/p/9803828.html 谢谢啦! 1.基本使用 因为Jedis工具类使用的是连接池,那么我们 ......
安装redis可以参考
redis的学习可以参考 谢谢啦!
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>
上一篇: Django之中间件