Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sharing of common interface implementation methods in redis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor tells you about the common interface implementation methods of redis, which many people don't know very well. today, in order to let you know more about the common interface implementation methods of redis, the editor summarizes the following contents, let's look down together. I'm sure you'll get something.

In practice, redis is usually used as a common tool class, you only need to call it, so here are some common interface methods and implementations of redis. Here are two different implementations that encapsulate the standalone version and the cluster version.

Java is developed based on Jedis's jar package. First of all, you need to download and install this jar package first, which is not provided here, but can be found directly on the Internet. If you build using maven, it is also very simple, as follows: what you rely on here is version 2.7.3, and there is no clear indication of which version to rely on, depending on your own situation.

Redis.clients jedis 2.7.3

1: common APIs for redis are as follows:

Public interface JedisClient {/ / method for assigning values String set (String key, String value); / / method for obtaining values String get (String key); / / determining whether Boolean exists (String key) exists; Long expire (String key, int seconds); Long ttl (String key); Long incr (String key) / / hset method Long hset (String key, String field, String value); / / hget method String hget (String key, String field); / / Delete has method Long hdel (String key, String...) Field); / / determine whether Boolean hexists (String key, String field) exists in has; List hvals (String key); / / delete method Long del (String key);}

2: the implementation of the stand-alone version of redis, 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 hvals (String key) {Jedis jedis = jedisPool.getResource (); List 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;}}

3: the implementation of 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 hvals (String key) {return jedisCluster.hvals (key) @ Override public Long del (String key) {return jedisCluster.del (key);}}

These are the details of the common interface implementation methods of redis. Do you have anything to gain after reading it? If you want to know more about it, welcome to the industry information!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report