Get the App
SLTechnology News&Howtos  ›  Database  › 

Examples of ​ Jedis's operations on five types of redis

Shulou Source: shulou.com Published: 2022-05-31 23:46:08 10月02日 Update

The purpose of this article is to share with you the content of examples of Jedis's five types of operations on redis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Jedis operates on five types of redis: strings, lists, hashes, collections, and ordered collections.

# # JedisUtil

The test case here is run with junit4, and the preparation code is as follows:

Private static final String ipAddr = "10.10.195.112"; private static final int port = 6379; private static Jedis jedis= null; @ BeforeClass public static void init () {jedis= JedisUtil.getInstance () .getJedis (ipAddr, port);} @ AfterClass public static void close () {JedisUtil.getInstance () .closeJedis (jedis,ipAddr, port);}

JedisUtil is a simple encapsulation of jedis. The code is as follows:

Import org.apache.log4j.Logger;import java.util.HashMap;import java.util.Map;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class JedisUtil {private Logger logger = Logger.getLogger (this.getClass (). GetName ()); private JedisUtil () {} private static class RedisUtilHolder {private static final JedisUtil instance = new JedisUtil ();} public static JedisUtil getInstance () {return RedisUtilHolder.instance } private static Map maps = new HashMap (); private static JedisPool getPool (String ip, int port) {String key = ip+ ":" + port; JedisPool pool = null; if (! maps.containsKey (key)) {JedisPoolConfig config = new JedisPoolConfig (); config.setMaxActive (RedisConfig.MAX_ACTIVE); config.setMaxIdle (RedisConfig.MAX_IDLE) Config.setMaxWait (RedisConfig.MAX_WAIT); config.setTestOnBorrow (true); config.setTestOnReturn (true); pool = new JedisPool (config,ip,port,RedisConfig.TIMEOUT); maps.put (key, pool);} else {pool = maps.get (key);} return pool } public Jedis getJedis (String ip, int port) {Jedis jedis = null; int count = 0; do {try {jedis = getPool (ip,port). GetResource () } catch (Exception e) {logger.error ("get redis master1 failed!", e); getPool (ip,port) .principnBrokenResource (jedis);}} while (jedis = = null & & count

Tags: Element content multiple time number integer score result output sort type command subscript interval ascending order data surplus storage ordering between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft NVidia Linux Shulou Tech Info