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

Summary notes on common operations of Jedis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

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

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