Get the App
SLTechnology News&Howtos  ›  Database  › 

Summary notes on common operations of Jedis

Shulou Source: shulou.com Published: 2022-06-01 13:12:37 10月04日 Update

Public class TestRedis {private Jedis jedis; @ Before / / Junit Note public void setup () {/ / Connect to the redis server, 192.168.0.100 public void setup 6379 / / jedis object instantiation two ways jedis = new Jedis ("192.168.0.100", 6379); jedis = RedisClient.getResource (); / / permission authentication jedis.auth ("admin") } / * redis storage string * / @ Test public void testString () {/ /-add data-jedis.flushDB (); jedis.set ("name", "annie"); / / put value-- > System.out.println ("name") into key-- > name / / execution result: xinxin jedis.setnx ("name", "bill"); / / No System.out.println (jedis.get ("name")); jedis.append ("name", "is my a girl"); / / stitching System.out.println of value (jedis.get ("name")) Jedis.del ("name"); / / delete a key System.out.println (jedis.get ("name")); / / set the expiration time (in seconds, jedis.setex ("sql", 2, "mysql"); System.out.println (jedis.get ("sql") Try {Thread.sleep (3000);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println (jedis.get ("sql")); / / set multiple key-value pairs jedis.mset ("name", "annie", "age", "22", "tel", "1517252pairs *") Jedis.incr ("age"); / add 1 operation / / mget returns the list collection System.out.println (jedis.mget ("name", "age", "tel")) } / * jedis operation List * / @ Test public void testList () {/ / first remove all content jedis.del ("framework"); System.out.println (jedis.lrange ("framework", 0mairel1)); / / store three pieces of data jedis.lpush ("framework", "spring") from the framework header Jedis.lpush ("framework", "struts"); jedis.lpush ("framework", "hibernate"); / / the first is key, the second is the start position, the third is the end position,-1 means to get all the System.out.println (jedis.lrange ("framework", 0mam 1); System.out.println ("framework", 0j2)) / / contains 2 subscript / / jedis.llen ("framework"); / / gets the list length jedis.del ("framework"); / / stores three pieces of data jedis.rpush ("framework", "spring") from the tail of framework; jedis.rpush ("framework", "struts") Jedis.rpush ("framework", "hibernate"); System.out.println (jedis.lrange ("framework") / / linsert adds a string element / / eg:linsert mylist3 before "world"there" / / redis 127.0.0.1 list 6379 > lrange mylist3 0-11) "hello" 2) "there" before or after the specific position of the corresponding list in the eg:linsert mylist3 before 3) "world" / / get the list length System.out.println (jedis.llen ("sql")) / sort / / System.out.println (); jedis.lset ("sql", 0, "oracle"); / / get the value System.out.println (jedis.lindex ("sql", 0) whose subscript is 0 for a list / / delete data / / lrem delete count same elements as value from the corresponding list of key / / eg: lrem mylist5 2 "hello" / / count > 0: delete count from beginning to end

Tags: Element subscript data number position statistics header character string tail difference mode length three same from beginning to end from small to large three middle intersection Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information Xiaomi macOS Apple