Experience _php tips on using the Key/value database Redis and Ttserver

Source: Internet
Author: User
Tags memcached posix redis

The

first says that Redis
Redis is a memcached-like key/value storage system that supports a relatively large number of stored value types, including string (string), list (linked list), set ( Collection) and Zset (ordered set). On this basis, Redis supports a variety of different ways of ordering. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modification operation to the appended record file (this person feels that Redis is more secure than the memcache in the data Save), and on this basis master-slave (Master-Slave) synchronization is achieved.

Redis has high access performance, set operations 110,000 times per second, and get operations 81,000 times per second (fast!). )。 The
Redis provides different commands for different storage-type objects. The
Redis currently offers four types of data: String,list,set and Zset (sorted set). The
string is the simplest type, one that you can understand as a type with memcached, a key corresponding to a value, and the operations supported on it are similar to those of memcached. But it's more functional. The

List is a list structure with the main function push, pop, get all the values of a range, and so on. The key in the operation is understood as the name of the list. The
set is a collection, similar to the set concept in our mathematics, which adds deletion elements to the operation of the set, and orthogonal poor operations on multiple sets. The key in the operation is understood as the name of the collection.

Zset is an upgraded version of Set, which adds an ordered attribute to the set, which can be specified when adding a modified element, and Zset automatically adjusts the order of the new value each time it is specified. You can understand that there are two columns of MySQL table, a list of stored value, a list of stored order. The key in the operation is understood as Zset's name.

The Redis command is provided below

:
for all types of commands
EXISTS key to determine whether a key exists, there is a return 1; DEL key Deletes a key, or a series of Key;del key1 key2 key3 key4
Type key returns the data type of a key element (none: No, String: Character, List,set,zset,hash)
Keys pattern returns a matching key list (keys foo*: Find the keys at the beginning of foo)
Randomkey randomly obtains an existing key and returns an empty string if the current database is empty
RENAME oldname NewName change the name of the key, if the new key exists will be overwritten
Renamenx oldname newname Change the name of the key, if the name exists then change failed
Dbsize return the total number of keys in the current database
EXPIRE Sets the expiration time (in seconds) of a key, (EXPIRE Bruce 1000: Set Bruce this key1000 seconds after the system automatically deletes) note meaning: If the value has not been expired, then the value will be cleared. The
TTL finds out how long it has expired for a key, returns time seconds
Select Index Select Database
Move key Dbindex The specified key from the current database to the target database Dbindex. Successful return of 1, otherwise return 0 (the source database does not exist key or the target database already has the same name key);
Flushdb empties all keys in the current database
Flushall clears all keys in all databases

the command
set key value that handles a string sets a string value for a key. Set KeyName datalength data (set Bruce Paitoubing: Save Key is Burce, string length is 10 of a string paitoubing to the database), the data maximum can not exceed 1G. The
Get key gets the value of a key. If the key does not exist, the string "nil" is returned and an error is returned if the value of the key is not a string type. The

Getset key value can be understood as the value of the obtained key and then set this value, more convenient operation (set Bruce Paitoubing, This time you need to change the Bruce into 1234567890 and get this previous data paitoubing,getset Bruce 1234567890
mget key1 key2 ... Keyn returns the value of multiple keys at once

The difference between Setnx key value Setnx and set is that set can create and update key value, and SETNX is if key does not exist, create key and value data
mset key1 Value1 key2 value2 ... keyn valuen set multiple keys and values at one time under atomic operation
Msetnx key1 value1 key2 value2 ... keyn valuen to set multiple keys and values at once under atomic operation (Target key If there is not a case, if more than one key already exists, it fails
INCR key self-added value
incrby key integer value self-specifying value
decr key self-subtraction key value
decrby Key integer to specify numeric values

handles lists's command
rpush key value adds an element from the end of the List (if the sequence does not exist, it is created first, if a key with the same name is already present, but not a sequence, an error is returned)
Lpush key value Add an element from the list header
Llen key returns the length of a list
Lrange key start to return the elements of the sequence from a custom range (Lrange testlist 0 2; return sequence testlist top 0 1 2 elements)
LTRIM key start to trim data outside a range (LTRIM testlist 0 2; Keep 0 1 2 elements, remaining deleted)
Lindex key index returns the sequence value of a location (lindex testlist 0; returns Sequence testlist element with position 0)
LSET key index value updates the value of a location element
Lrem key count value from the List Head (count positive) or tail (count negative) deletes a certain number (count) of elements that match value, and returns the number of deleted elements.
Lpop key Pop-up list first element
Rpop key pop-up list last element
Rpoplpush srckey Dstkey eject the last element of the _srckey_ and press it into the _DSTKEY_ header, key Does not exist or the sequence is empty, returns "nil"

A command that handles the collection (sets)
sadd key member to increase the element to the sets sequence, and if the element (Membe) does not exist, add success 1, or failure 0; (Sadd Testlist 3/n one)
Srem key member deletes an element of the sets sequence and fails 0 if the element does not exist, otherwise the Success 1 (Srem testlist 3/n a)
Spop key pops up a members randomly from the collection
smove Srckey Dstkey member moves an element of a sets sequence to another sets sequence (Smove testlist test 3/n two; move element testlist from sequence two to test, testlist There will be no two element)
SCard key counts the number of elements in a sequence of a sets
Sismember key member learns whether the specified members exist in the collection
Sinter key1 key2 ... keyn return key1, Key2, ..., the intersection in Keyn
Sinterstore dstkey key1 key2 ... Keyn key1, Key2, ..., the intersection in Keyn is saved Dstkey
sunion key1 key2 ... keyn return key1 , Key2, ..., Keyn's and set of
Sunionstore dstkey key1 key2 ... Keyn key1, Key2, ..., Keyn's and set into Dstkey
Sdiff key1 key2 ... keyn basis Key2, ..., Keyn to seek key1 difference set. Official example:
Key1 = x,a,b,c
Key2 = c
Key3 = a,d
Sdiff key1,key2,key3 => x,b
sdiffstore dstkey key1 key2 ... Keyn According to Key2, ..., keyn the difference set of Key1 and deposit Dstkey
Smembers key Returns all elements of a sequence
Srandmember key randomly returns the elements of a sequence

Commands for processing ordered collections (sorted sets) (Zsets)
Zadd key score member to add specified members to an ordered set and update score if the target exists (score, sort)
Zrem key member deletes specified members from an ordered collection
Zincrby Key Increment member adds _increment_ if a member is present, or it sets a score as _increment_
Zrange Key start end returns a member of the specified range after ascending sort
Zrevrange Key start end returns a member of the specified range after descending sort
Zrangebyscore key min Max returns all member <= key that conforms to score >= min and score Zcard Max returns an ordered set of elements Zscore key element returns the score value of the specified member Zremrangebyscore Key min Max deletes all members that meet the score >= min and score <= max conditions.

Usage Experience:
Personally feel that Redis speed is needless to say (soon), but very consuming physical memory, is a redis of the disadvantage of it, redis suitable for the relatively small amount of data to update a fast type of Web site, such as the community, not suitable for large data sites, such as forums. Previously used Redis application of a forum post, but because the amount of data is too large, the consumption of physical memory is amazing and give up the use of redis!

the Ttserver
Tokyo Cabinet is the implementation of a dbm. The database here is composed of a series of key-value pairs of records. Both the key and value can be sequences of bytes of any length, either binary or string. There is no concept of data types and data tables. When used for a hash table database, each key must be different so that two key values cannot be stored. The following access methods are provided: provide Key,value parameters to store, delete records by Key, and read records by key, in addition, traversal key is also supported, although the order is arbitrary can not be guaranteed. These methods are the same as those of the UNIX standard dbm, such as GDBM,NDBM, but they are much better than their performance (so they can be substituted) when a B + tree is stored, a record with the same key can be stored. Like a hash table, read, store, delete functions are also available. Records are stored according to user-supplied comparison functions. You can read each record by using a sequential or reverse-order cursor. According to this principle, the forward string matching search and the integer interval search are also implemented. In addition, a B + Tree transaction is also available. For a fixed-length array, records are labeled as natural numbers for storage. You cannot store two or more records with the same key. In addition, the length of each record is limited. The reading method is the same as the hash table. Tokyo cabinet is written in C and provides C,perl,ruby,java APIs. Tokyo cabinet is available on a platform that provides POSIX and C99, and it is published in the GNU Lesser Public License protocol.

tokyocabinet: a key-value dbm database, but does not provide a network interface, hereinafter referred to as TC.
tokyotyrant: is a network interface written for TC, he supports the Memcache protocol, or it can be done via HTTP, and the following is called TT.

Performance:
Tokyo Cabinet is a DBM database developed by the Japanese flat Lim, Tokyo Cabinet based on the GNU Lesser General public Li The Cense protocol, which is developed in C, can be run on any support C99 and POSIX platform. Compared to the general DBM database has the following features: small space, high efficiency, high performance, high reliability, the support of various development languages (now provided C,perl,ruby,java,lua API), support 64-bit operating system. The database reads and writes very fast, the hash mode writes 1 million data only in 0.643 seconds, reads 1 million data to only need 0.773 seconds, is Berkeley DB and so on DBM several times.

Tokyo tyrant plus Tokyo Cabinet, which constitutes a distributed, persistent storage system that supports high concurrency, can be considered memcached Tokyo for any original tyrant client, but Its data can be stored for a long duration. This, like Sina's memcachedb nature.

Ttserver and Memcache comparisons:
ttserver is a database, memcached is a cache. Both are stored in the form of data <key,value>, through the key to do any action. Ttserver can persist data, memcached all are stored in memory, Memcached automatically deletes expired data for a maximum of 30 days. Memcached in conjunction with some APIs, automatic data access serialization, read deserialization. Ttserver has the function of master-slave copy, operation log, etc., this is a database only thing. It is said that memcached is adjusting to the overall architecture and supporting the plugin mechanism. The network, event processing, memory storage will be stripped away. You can write a storage engine later to do a disk-based key-value storage. Memcached's two development has stepped into a small climax.

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.