First knowledge of Redis

Source: Internet
Author: User

 PackageCom.wangzhu.redis;Importjava.util.List;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;/*** Redis is a key-value storage system. Similar to memcached, it supports storage of value types relative to more,<br/> * including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). <br/> * These data types support Push/pop, Add/remove, and intersection sets and differences, and richer operations, and these operations are atomic in nature. <br/> * Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. <br/> * The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and implements the Master-slave on this basis. <br/> * Required jar package: Jedis-2.6.2.jar hamcrest-core-1.3.jar junit-4.11.jar<br/> * *@authorWangzhu * @date 2015-3-9 a.m. 9:10:16 **/ Public classTestredis {Jedis Jedis;    Jedispool Pool; @Before Public voidinit () {Pool=NewJedispool (NewJedispoolconfig (), "localhost"); Jedis=Pool.getresource (); } @Test Public voidtestping () {System.out.println ("Servier is runnning:" +jedis.ping ()); //Servier is Runnning:pong} @After Public voiddestroy () {jedis.disconnect (); } @Test Public voidteststring () {//GetSystem.out.println ("get===" + jedis.get ("Strkey"))); //SetSystem.out.println ("set===" + jedis.set ("Strkey", "Qingyezhu")); //GetSystem.out.println ("get===" + jedis.get ("Strkey"))); //DeleteSystem.out.println ("del===" + Jedis.del ("Strkey"))); //Get===null//Set===ok//Get===qingyezhu//del===1} @Test Public voidtestlist () {String ListKey= "ListKey"; //System.out.println ("lpush==="//+ jedis.lpush (ListKey, "Redis", "MongoDb", "MYSQL" , "Oracle",//"DB2"));System.out.println ("lpush====" + Jedis.lpush (ListKey, "other")); LongLen =Jedis.llen (ListKey); System.out.println ("size===" +Len); List<String> list = Jedis.lrange (ListKey, 0, Len); System.out.println ("list====" +list);  for(inti = 0; i < Len; i++) {System.out.println (i+ "===lindex===" +Jedis.lindex (ListKey, i));    } System.out.println (Jedis.llen (ListKey)); }}
1, string operation set key value set key value get key values get key set expiration number of milliseconds Pexpire key10000View the life cycle of a key (MS) Pttl key set the lifetime of key (seconds) expire key1View key lifetime (seconds) TTL key to set the specified key to permanently valid perisist key Delete a Keydel key return random keyrandomkey return key stored type key renamed Rename key Newkey set multiple values at once Mset key1 value Key2 value Key3 value get more than one value at a time mget key1 Key2 Key3 appends value to the original value of key Append key value takes the value of the specified key Length strlen Key2, linked list operation the list type of Redis is actually a doubly linked list where each child element is a string type, and the maximum length of the linked list is 2^32. A list can be used as a stack or as a queue. The pop operation of the list also has a blocking version, primarily to avoid polling. Inserts the value into the list header Lpush key value inserts the value into the end of the list Rpush key value returns and deletes the list header element Lpop key returns and deletes the linked list trailing element Rpop key returns the element in the list [start,stop] Lrange key Start stop counts the number of elements in the list Llen key returns the value on index lindex key index3, hashed type operation Redis Hash is a string-type field and value mapping table, and its add, delete operations are O (1) (average). Hash is especially useful for storing objects, storing an object in a hash type consumes less memory, and provides easy access to the entire object. Configuration: #配置字段最多64个hash_max_zipmap_entries64#配置value最大为512字节hash_max_zipmap_value512set hash1 field to Valuehset hash1 field value to get the specified hash fieldhget hash1 field setting multiple Fieldhmset hash1 field1 value1 Field2 V Alue2 field3 value3 Get multiple fieldhmget hash1 field1 field2 field3 test developed hash field whether there is hexists hash1 field gets hash field number Hlen HASH1 return hash All the Fieldhkeys hash1 return hash all valuehvals HASH1 get a hash of all the field and Valuehgetall hash1 Delete the specified Fieldhdel hash1 Field4, set structure operation characteristics: disorder, certainty, uniqueness add elements to the collection Sadd key value1 value2 value3 Get collection all elements smembers key Delete Collection an element Srem key Value returns and deletes a random element in the collection Spop key randomly takes an element srandmember key to determine if there is a value in the collection Sismember key value returns the number of elements in the collection SCard key for Key1 Key2 Key3 intersection sinter key1 key2 Key3 seek Key1 Key2 's set sunion key1 key2 seek key1 key2 difference set Sdiff key1 key2 seek key1 key2 intersection and exist res sinterstore res K Ey1 Key2 Moves the value of source to the Dest collection Smove source dest value6, an ordered set concept: It adds a sequential attribute on the set basis, which can be specified when adding modified elements, and Zset automatically adjusts the order of the new values after each assignment. add element zadd key score1 value1 query member ranking Zrank key member returns the number of collection elements Zcard key removes elements from the collection Zrem key value1 value2

Reference:

Redis memory usage optimization and storage "http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage"

Installation of Redis under Windows uses "Http://os.51cto.com/art/201403/431103.htm"

First knowledge of Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.