Experience with Redis and ttserver with Key/value database _php tutorial

Source: Internet
Author: User
First, 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 (set), and Zset (ordered collections). Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to the disk or writes the modification to the appended record file (craved personally feels that Redis is safer than memcache in data preservation), and on this basis implements Master-slave (Master-slave) synchronization.

Redis has high access performance, set operations 110,000 times per second, get operations 81,000 times per second (very cool!). )。
Redis provides different commands for different types of storage objects.
Redis currently offers four types of data: String,list,set and Zset (sorted set).
String is the simplest type, you can understand it as a type with memcached, a key corresponds to a value, and the operation supported on it is similar to the operation of Memcached. But it's more versatile.

List is a linked list structure, the main function is push, pop, get all the values of a range and so on. Key in operation is understood as the name of the linked list.
Set is a set, and we are similar to the concept of the set in mathematics, the operation of the set has added delete elements, there are multiple sets of orthogonal and poor operation. In Operation Key is understood as the name of the collection.

Zset is an upgraded version of set, and he adds a sequential attribute on the set, which can be specified when adding a modified element, and Zset automatically re-adjusts the order of the new values after each assignment. It can be understood that there are two columns of MySQL table, one column of value, and one in the order of storage. Key in operation is understood as the name of Zset.

The Redis command is provided below:
For all types of commands
EXISTS key determines whether a key exists; returns 1; otherwise returns 0;
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: Not present, string: Character, List,set,zset,hash)
Keys pattern Returns a list of matching keys (keys foo*: Find keys at the beginning of Foo)
Randomkey randomly obtains a key that already exists and returns an empty string if the current database is empty
RENAME oldname newname Change key name, new key if present will be overwritten
Renamenx oldname newname Change the name of the key and the change fails if the name exists
Dbsize returns the total number of keys for the current database
EXPIRE set the expiration time of a key (in seconds), (EXPIRE Bruce 1000: Set Bruce this key1000 seconds after the system is automatically deleted) Note : If the value has not expired, then the value will be cleared.
How long does the TTL look for a key expire and return seconds
Select Index Selection Database
Move key Dbindex moves the specified key from the current database to the target database Dbindex. Successful return 1, otherwise 0 (the source database does not exist key or the target database already exists with the same name key);
FLUSHDB clears all keys in the current database
Flushall clears all keys in all databases

The

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

Getset key value can be interpreted as the value of the obtained key and set this value for a more convenient operation (set Bruce Paitoubing, This time you need to change Bruce to 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 if key does not exist, create key and value data
MSET key1 Value1 key2 value2 ... keyN valuen set multiple keys and values at once in an atomic operation
Msetnx key1 value1 key2 value2 ... keyN valuen set multiple keys and values once in one atom operation (target key Does not exist, if more than one key already exists, it fails)
INCR key self-increment value
incrby key integer to increment the value from the specified value
decr key self-decrement value
decrby Key integer to subtract the specified value from the value

lists 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 exists instead of a sequence, then 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 end returns the elements of a sequence from a custom range (Lrange testlist 0 2; return sequence testlist first 0 1 2 elements)
LTRIM key start end trims data outside a range (LTRIM testlist 0 2; Leave 0 1 2 elements, remaining deleted)
LINDEX key index returns the sequence value of a position (LINDEX testlist 0; return Sequence testlist element with position 0)
LSET key index value to update values for a location element
Lrem key count from List The head (count positive) or tail (count negative) deletes a certain number (count) of elements that match value, returning the number of deleted elements.
Lpop key pops up the first element of the list
Rpop key pops up the last element of the list
Rpoplpush Srckey dstkey pops the last element of the _SRCKEY_ and presses it into the _DSTKEY_ header, key Does not exist or the sequence is empty returns "nil"

The

handles the collection (sets) command (with index unordered sequence)
sadd Key member adds the element to the sets sequence, if the element (Membe) does not exist then adds success 1, otherwise fails 0; (Sadd Testlist 3/n one)
Srem key member Delete an element of the sets sequence if the element does not exist and fails 0, otherwise success 1 (Srem testlist 3/n one)
SPOP key randomly pops a member from the collection
smove Srckey Dstkey member moves an element of a sets sequence to another sets sequence (Smove testlist test 3/n Two; moves the element from sequence testlist to test, testlist There will be no both elements)
SCard key counts the number of elements of a sequence of sets
Sismember key member learns whether the specified member exists in the collection
SINTER key1 key2 ... keyN return key1, Key2, ..., intersection in KeyN
Sinterstore dstkey key1 key2 ... KeyN will Key1, Key2, ..., intersection in KeyN dstkey
sunion key1 key2 ... keyN return key1 , Key2, ..., KeyN's sunionstore dstkey key1 key2 ... KeyN will Key1, Key2, ..., KeyN set to Dstkey
Sdiff key1 key2 ... keyN basis Key2, ..., KeyN seek the difference of key1. 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 handling ordered collections (sorted sets) (Zsets)
Zadd Key Score member adds the specified member to the ordered collection, and updates the score (score, sort) if the target exists
Zrem Key member deletes the specified member from an ordered collection
Zincrby Key Increment member adds _increment_ If member exists, otherwise a member score to _INCREMENT_ will be set
Zrange Key start end returns the member of the specified range after ascending order
Zrevrange Key start end returns the member of the specified range after descending sort
Zrangebyscore key min Max returns all members that conform to score >= min and score <= Max Zcard key Returns the number of elements in an ordered collection Zscore key element returns the score value of the specified member Zremrangebyscore Key min Max deletes all members that meet score >= min and score <= max criteria.

Usage Experience:
Individuals in the sense that redis speed is needless to say (soon), but very much of the physical memory, is a redis disadvantage, Redis is suitable for the data volume relatively small speed update fast type of site, such as the community, not suitable for large data sites, such as the Forum. A previously used Redis app on a forum post, but because the amount of data is too large, the consumption of physical memory amazing and abandoned with redis!

say Ttserver
Tokyo Cabinet is an implementation of dbm. The database here consists of a series of key-value pairs of records. Both key and value can be byte sequences of any length, either binary or string. There is no concept of data types and data tables. When used as a hash table database, each key must be different, so it cannot store two keys with the same value. Provides the following access methods: Provide the Key,value parameter to store, delete the record by key, press key to read the record, in addition, traverse key is also supported, although the order is arbitrary can not be guaranteed. These methods are identical to Unix-standard dbm, such as GDBM,NDBM, and so on, but are much better than their performance (and therefore can be substituted for them) when stored in a B + tree, records with the same key can be stored as well. Read, store, and delete functions like hash tables are also available. Records are stored according to user-provided comparison functions. Each record can be read in either sequential or reversed cursors. In accordance with this principle, forward string matching search and integer interval search are also implemented. In addition, the business of B + Tree is also available. For fixed-length arrays, records are marked for storage by natural numbers. You cannot store two or more records of the same key. Additionally, the length of each record is limited. The Read method is the same as the hash table. The Tokyo cabinet is written in C and also provides the C,perl,ruby,java API. The Tokyo cabinet is available on both POSIX and C99 platforms, and it is released under the GNU Lesser Public License protocol.

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

Performance:
Tokyo Cabinet is a DBM database developed by Japanese Ping Lin, and Tokyo Cabinet is based on the GNU Lesser General Public License Protocol, developed in C language and can be run on any supported C99 and POSIX platform Use. Compared to the general DBM database has the following characteristics: small space, high efficiency, high performance, high reliability, a variety of development language support (now available C,perl,ruby,java,lua API), support 64-bit operating system. The database reads and writes very fast, the hash pattern is written to 1 million data in just 0.643 seconds, and 1 million data is only 0.773 seconds, which is several times the Berkeley DB and other DBM.

Tokyo Tyrant plus Tokyo Cabinet, a distributed persistent storage system that supports high concurrency, can be viewed as a memcached for any original memcached client, but Its data can be stored durably. This, like Sina's memcachedb nature.

Ttserver and Memcache comparison:
Ttserver is a database, memcached is a cache. Both are saved in the form of data, and any operation via key. Ttserver can persist data persisted, memcached are all stored in memory, memcached will automatically delete expired data, up to 30 days. Memcached in conjunction with some APIs, it is possible to automate the data entry and exit serialization, and read the deserialization. Ttserver has the function of master-slave replication, operation log, etc., this is a database only thing. It is said that memcached is making adjustments to the overall architecture and will support the plugin mechanism at that time. The network, event processing, and memory storage are stripped away. Later, to do disk-based Key-value storage, you can write a storage engine. Memcached's two-time development has stepped into a small climax.

http://www.bkjia.com/PHPjc/327945.html www.bkjia.com true http://www.bkjia.com/PHPjc/327945.html techarticle First, 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 (set), and Zset (ordered set ...) .

  • 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.