Redis learning records and redis records

Source: Internet
Author: User
Tags object serialization

Redis learning records and redis records
Redis is a key-value storage system. Similar to Memcached, Memcached supports more storage value types, including string, list, set, and zset) and hash (hash type ). These data types support push/pop, add/remove, Intersection Set and difference set, and more abundant operations, and these operations are atomic. On this basis, redis supports sorting in different ways. Like memcached, data is cached in the memory to ensure efficiency. The difference is that redis periodically writes the updated data to the disk or writes the modification operation to the append record file, and on this basis implements master-slave (master-slave) synchronization. Redis is a high-performance key-value database. The emergence of redis largely compensates for the shortage of key/value storage such as memcached, and can play a good complementary role in relational databases in some cases. It provides clients such as Java, C/C ++, C #, PHP, JavaScript, Perl, Object-C, Python, Ruby, and Erlang for ease of use. [1] Redis supports master-slave synchronization. Data can be synchronized from the master server to any number of slave servers. The slave server can be the master server associated with other slave servers. This allows Redis to perform single-layer tree replication. You can intentionally or unintentionally write data to a disk. Because the publishing/subscription mechanism is fully implemented, you can subscribe to a channel and receive the complete message publishing records of the master server when synchronizing the tree from the database anywhere. Synchronization is helpful for the scalability and data redundancy of read operations. The official redis website address is redis. io. (I specifically checked that the domain name suffix I/O belongs to the national domain name, which is the british Indian Ocean territory, that is, the british Indian Ocean territory)

Currently, Vmware is funding the development and maintenance of redis projects.

Five basic types: String hash list set zsetstringsstring is the simplest type, it is binary secure and contains any data parts and Object serialization set name helloget keysetnx. If k exists, 0 nx note exist is returned and not overwritten, there is no substring set new value setnx name hellosetex set k value to string type value and specify validity period setex namex 10 jak 10 s validity period setrange set value for specified key get namexiaomingxxxxxx@126.comsetrange name 8 aaaaaa replacing the specified length from the subscript with the same name as the replacement get namemset setting the value of multiple keys at a time fails. mset k1 xiaoming k2 xiaohongmsetnx note exis will not overwrite the value of the existing keygetset set key and return the old value of the key getset k1 hellogetrange to obtain the value of the key. The substring getrange k1 0 3 is returned from 0 to 3rd. mget batch gets mget k1 k2incr incremental set k3 10 incr k3incrby k4 10 if the specified value key does not exist, the key is set to think that the original value is 0 incrby k4-10 decr to subtract the value of the key and decrby to specify the append to append valueappend k1 world to the string of the specified key. return length strlen return value length strlen k1

The hashset type is a string-type field-value ing table. Its Addition and deletion operations are all 0 (1) (average) hash, which is particularly suitable for storing objects, compared with saving each field of an object into a single string type, it will be used in the hash of an object for a moment, in addition, you can easily access the entire object hset the hash field to the specified value. If the key does not exist, create hset myhash field hellohset user: 001 name jakhget user: 001 namehsetnx user: 002 name hellohmset batch set value hmset myhash field hello field2 worldhmet get all healthy hmet user: 002 name age x batch return hincrby user: 002 age 10 auto-increment hexists hash table field whether hexists user: 002 agehlen returns the number of keys in the specified hash hlen user: 002 hdel Delete the field hdel myhash agehkeys of the specified hash return all hash fields hkeys user: 002 hvals return all hash valuehvals user: 002 hgetall get all fields and values of a hash hgetall user: 002


========================== List start ================================ ======== list is a linked list structure, the main function is push, pop, get all values in a range, etc. In the operation, the key is interpreted as the name of the linked list. The list type of redis is actually a two-way linked list of the string type for each sub-element. We can add and delete elements from the head or tail of the linked list through the push pop operation. In this way, the list can be used as a stack and a queue. Push to an element pop up an element lpush is pushed from the header to an element first, then the lpush mylist worldlpush mylist hellolrange mylist 0-1 get the element hello worldrpush press the element from the end, first the queue rpush list2 jakrpush list2 tomlrange list2 0-1 jaktomlinsert Add the string linsert list2 before jak sumlset before or after the specific location of the key corresponding to the list to set the specified subscript element in the list to replace the lset list2 1 updatevlrem from the key corresponding list delete N elements with the same value (n <0 is deleted from the end, N = 0 Delete All) lrem list2 jak delete several elements ltrim keep data within the value range of the specified key ltrim list2 1-1 Delete 0th lpop Delete elements from the list header and return the delete element lpop list2 rpop an element rpop list2lpoplpush pops up from the end to remove the element from the end of the first list and adds it to the header of the second list. lpoplpush list2 list3lindex returns the linex list2 0 element in the index position of the list named key. lindex list2 1 llen returns the length of the list corresponding to the key. sllen list2 ====================== list end ====== ==============================

============== Sets unordered set ==================== sadd myset1 onesadd myset1 twosadd myset1 two cannot add duplicates value smembers myset1 view element srem Delete srem myset1 onespop random pop-up element sdiff returns the sdiff of all given keys and the first key sdiff myset1 distinct storage difference set value sdiffstore myset3 myset1 myset2sinter intersection sinter myset2sinterstore stores the intersection value sunion returns the union of all given keys sunion myset1 myset2sunionstore stores and sets smove to remove elements from the set corresponding to the first key and add them to the second set of the second pair. smove myset1 myset2 three moves three from 1 to 2. scard returns the number of sets named key. sismember tests whether the element is a Set element named key. sismember myset1 two returns 1, indicating that sismember myset1 twox returns 0, not srandmember returns an element of the set at random, but do not delete the element srandmember myset1 ==========================



=============== Sorted sets sorted set ================================ ==== is an upgraded version of the set, and a sequence attribute zset is automatically re-ordered, it can be understood that there are two columns of mysql table, one column of storage value and one column of order, during the operation, the key can be understood as zadd to add the element member to the zset named key, score is used for sorting. If this element exists, update the zadd myzset 1 onezadd myzset 2 twozadd myzset 3 two will update the order to 3 zrange myzset 0-1 withscores 0 first location-1 last zrem delete the name key the element memberzrem myzset1 onezincrby in zset is added with the specified value. If the element memebre already exists in the zset named key, the score of this element is added with incremernt. Otherwise, this element is added to this set, the score value is incrementzincrby myzset 4 two. The sequence number is increased to minus zrank. The ranking of memebr elements in the zset named key is returned (sorted by score in ascending order) that is, the subscript zrank myzset1 two



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.