In actual work, redis is usually used as a commonly used tool class. You only need to call it, so here are some common interface methods and implementations of redis. Here are two different implementations encapsulating the stand-alone version and the cluster version.
java is developed based on the Jedis jar package. First of all, you need to download and install the jar package. It is not provided here. You can directly search online. If you use maven to build, it is also very simple, as follows: The dependency here is version 2.7.3. There is no clear rule on which version this depends on, it depends on your own situation.
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency>
One: The common interfaces of redis are as follows:
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); //hset 方法 Long hset(String key, String field, String value); //hget方法 String hget(String key, String field); //删除 has方法 Long hdel(String key, String... field); //判断has是否存在 Boolean hexists(String key, String field); List<String> hvals(String key); //删除方法 Long del(String key); }
Two: The implementation of the redis stand-alone version, the code is as follows:
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; public class JedisClientPool implements JedisClient { private JedisPool jedisPool; public JedisPool getJedisPool() { return jedisPool; } public void setJedisPool(JedisPool jedisPool) { this.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 result = jedis.hdel(key, field); jedis.close(); return result; } @Override public Boolean hexists(String key, String field) { Jedis jedis = jedisPool.getResource(); Boolean result = jedis.hexists(key, field); jedis.close(); return result; } @Override public List<String> hvals(String key) { Jedis jedis = jedisPool.getResource(); List<String> result = jedis.hvals(key); jedis.close(); return result; } @Override public Long del(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.del(key); jedis.close(); return result; } }
Three: The implementation of the redis cluster version: the specific code is as follows :
import redis.clients.jedis.JedisCluster; public class JedisClientCluster implements JedisClient { private JedisCluster jedisCluster; public JedisCluster getJedisCluster() { return jedisCluster; } public void setJedisCluster(JedisCluster jedisCluster) { this.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) { return jedisCluster.hdel(key, field); } @Override public Boolean hexists(String key, String field) { return jedisCluster.hexists(key, field); } @Override public List<String> hvals(String key) { return jedisCluster.hvals(key); } @Override public Long del(String key) { return jedisCluster.del(key); } }
For more redis knowledge, please pay attention to the redis introductory tutorial column.
The above is the detailed content of Redis common interface implementation. For more information, please follow other related articles on the PHP Chinese website!