Redis Database 2

Source: Internet
Author: User
Tags connection pooling set set

A. Storage list
1. Overview
The list type is a list of strings sorted by insertion order, which can be added to the head (left) and the tail (right) and automatically created if none exist, or the list disappears if all the elements in the list are removed;
ArrayList: The use of arrays to store data, there are indexes, so query fast, adding and deleting slow (adding and removing involves displacement operation);
LinkedList: For the two-way linked list, each element is recorded before and after the elements of the pointer, insert/delete only change the pointer before and after the element, faster, the query needs to be calculated from the beginning, the speed is relatively slow;
2. Common commands
2.1 Add
Lpush list01 a #头部添加 (left)
Rpush list01 b #尾部添加 (right)
2.2 views
Lrange list01 0-1 #查看从第1个到倒数第1个元素之间的所有元素, i.e. all elements;
Lrange list01 0-2 #查看从第1个到倒数第2个元素之间的所有元素;
2.3 Eject
Lpop list01 #弹出头部元素 (left);
Rpop list01 #弹出尾部元素 (right);
2.4 Getting the number of elements in a list
Llen list01
2.5 Add an element to the existing list only, and if the list does not exist, add the failure
Lpushx list01 C #头部添加
Rpushx list01 D #尾部添加
2.6 Delete
Lrem list01 2 a #删除list01中最左边的2个a元素;
Lrem List01-2 a #删除list01中最右边的2个a元素;
Lrem list01 0 A #删除list01中所有的a元素;
2.7 Modifications
LSet list01 3 M #设置list01中索引为3的元素为m; 0 represents the head element/1 for the tail element/index does not exist and throws an exception
2.8 inserting
Linsert list01 before 2 1 #在list01中的2元素左边插入元素1;
Linsert list01 after 2 1 #在list01中的2元素右边插入元素1;
2.9 Add head to the tail
Rpoplpush list01 list02 #将list01中的尾部元素添加到list02的头部;


Two. Store set
1. Overview
Set is an unordered collection, does not contain duplicate data, can be done on the server end of the collection of the intersection/and/or poor calculation, the efficiency is very high. When the increment/delete/judgment exists, the time complexity is O (1);
2. Common commands
2.1 Add
Sadd set01 a b c #如果set01中已有a, it is not added repeatedly
2.2 Delete
Srem set01 a #删除a元素
2.3 Getting the elements in the collection
Smembers set01 #获取set01中所有的key
2.4 Determine if an element exists in the collection
Sismember set01 A #判断set01中是否存在a元素, 1 means present, 0 means the element does not exist, or set01 does not exist
2.5 Differential Set operation
Sdiff set01 set02 #返回除去二者共有元素后, the remaining elements of the set01;
Sdiff set02 set01 #返回除去二者共有元素后, the remaining elements of the set02;
Sdiffstore set03 set01 set02 #将返回的元素存储在set03中;
2.6 Intersection operation
Sinter set01 set02 #返回二者共有的元素;
Sinterstore set03 set01 set02 #将返回元素存储在set03中;
2.7 Set operation
Sunion set01 set02 all elements #返回set01 set02 (repeating elements are displayed only once);
Sunionstore set03 set01 set02 #将并集运算结果存储在set03中
2.8 Randomly returns an element in the collection
Srandmember set01
2.9 Getting the number of elements in the collection
SCard set01


Three. Storage Sorted-set
1. Overview
Sorted-set is similar to the set set, which is a collection of strings and is not allowed to be duplicated; the difference is that each member in Sorted-set is associated with a fraction (score), sorted by fractions;
Although the elements are unique, the scores can be duplicated;
2. Common commands
2.1 Add
Zadd mysort Jack #向mysort的Sorted-set add Jack Element (score 20);
Zadd Mysort #可同时添加多个 of Rose Smith;
2.2 Getting a member score
Zscore Mysort Rose #返回字符串分数;
2.3 Getting the number of members
Zcard mysort #返回成员个数, integer type;
Zcount Mysort #获取分数在40-70 Number of members, returns an integer
2.4 Deleting an element
Zrem Mysort Smith #移除smith
Zrem Mysort Smith Jack #移除smith
Zremrangebyrank Mysort 1 3 #移除排名 (small to large) 1-3 elements
Zremrangebyscore Mysort 0 #移除分数在0-60 elements of this room
2.5 Range Queries
Zrange mysort 0-1 #返回从第一个到倒数第一个元素, i.e. all elements;
Zrange Mysort 0-1 with scores #返回从第一个到倒数第一个元素, and contains fractions;
Zrevrange Mysort 1 5 with scores #按照从大到小的顺序返回 (contains both ends of the element);
2.6 Returns the specified number of members, sorted by small to large score
Zrangebyscore mysort withscores limit 3 5 #首先抽出90-100 elements are sorted from small to large, starting with an index of 3, taking 5 elements, and returning
2.7 Modifying fractions
Zincrby Mysort 5 Jack #jack分数增加5
2.8 Returning member rankings
Zrank mysort Jack #返回成员在集合中的排名, Integer, from small to large;
Zrevrank mysort Jack #返回成员在集合中的排名, Integer, from big to small;

Four. Keys General operations
1.keys pattern: Get all the keys that match the pattern
Keys * # * denotes one or more characters, which means that all keys are returned;
Keys m? #? to match a character, this statement returns a key with a length of 2 characters beginning with M;
2. Delete the specified key
Del Mysort
Del list01
3. Determine if key exists
exists Mysort #1代表存在, 0 means not present
4. Renaming
Rename Mysort sort01 #把mysort重命名为sort01
5. Set the expiration time in seconds
Expire Mysort #设置mysort的过期时间为100s
6. Get the remaining time, if not set, return-1
TTL mysort
7. Get the type of key (String/list/set/hash/zset), does not exist return none
Type Mysort

Five. Jedis (the following code is implemented in eclipse)
1. Importing the jar package (commons-pool2-2.3/jedis-2.7)
2. Turn on Redis (./bin/redis-server./redis.conf) and test the connection success
3. Turn on port 6379
#防火墙打开3306端口
/sbin/iptables-i input-p TCP--dport 6379-j ACCEPT
#将打开的端口的访问规则保存在文件中
/etc/rc.d/init.d/iptables Save
4. Implement the following code

1  PackageJedisdemo;2  ImportOrg.junit.Test;3  ImportRedis.clients.jedis.Jedis;4  ImportRedis.clients.jedis.JedisPool;5  ImportRedis.clients.jedis.JedisPoolConfig;6 7  Public classJedisdemo {8@Test9       Public voidTest01 () {Ten          //Construct Jedis instance object OneJedis Jedis =NewJedis ("192.168.128.130", 6379); A          //Set Key-value -Jedis.set ("name", "Rose"); -          //Based on key to take value theString name = Jedis.get ("name"); -SYSTEM.OUT.PRINTLN (name); -          //Release Resources -Jedis.close (); +} -  +     //connection pooling mode A@Test at       Public voidtest02 () { -          //Get connection pool, set maximum number of connections, valid idle connection -Jedispoolconfig conf =NewJedispoolconfig (); -Conf.setmaxtotal (100); -Conf.setmaxidle (10); -          //Create a Jedis connection pool based on the connection pool configuration inJedispool pool =NewJedispool (conf, "192.168.128.130", 6379); -          //Get connection pool Get instance object toJedis Jedis = Pool.getresource (); +Jedis.set (" Age", " -"); -String age01 = Jedis.get (" Age"); theSystem.out.println (AGE01); *          //Close Resources $Jedis.close ();Panax NotoginsengPool.close (); -  the} +} A 

Redis Database 2

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.