Home  >  Article  >  Backend Development  >  Experience on using key/value database redis and TTSERVER_PHP tutorial

Experience on using key/value database redis and TTSERVER_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:13779browse

先说redis
redis是一个类似memcached的key/value存储系统,它支持存储的value类型相对较多,包括string(字符串)、 list(链表)、set(集合)和zset(有序集合)。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件(这点儿个人觉得redis比memcache 在数据保存上要安全一些),并且在此基础上实现了master- slave(主从)同步。

redis的存取性能很高,SET操作每秒钟 110000 次,GET操作每秒钟 81000 次(速度很爽!)。
Redis针对不同的存储类型对象提供了不同的命令。
redis目前提供四种数据类型:string,list,set及zset(sorted set)。
string是最简单的类型,你可以理解成与Memcached一模一个的类型,一个key对应一个value,其上支持的操作与Memcached的操 作类似。但它的功能更丰富。

list是一个链表结构,主要功能是push、pop、获取一个范围的所有值等等。操作中key理解为链表的名字。
set是集合,和我们数学中的集合概念相似,对集合的操作有添加删除元素,有对多个集合求交并差等操作。操作中key理解为集合的名字。

zset是set的一个升级版本,他在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的 值调整顺序。可以理解了有两列的mysql表,一列存value,一列存顺序。操作中key理解为zset的名字。

下面提供redis命令:
适合全体类型的命令
EXISTS key 判断一个键是否存在;存在返回 1;否则返回0;
DEL key 删除某个key,或是一系列key;DEL key1 key2 key3 key4
TYPE key 返回某个key元素的数据类型 ( none:不存在,string:字符,list,set,zset,hash)
KEYS pattern 返回匹配的key列表 (KEYS foo*:查找foo开头的keys)
RANDOMKEY 随机获得一个已经存在的key,如果当前数据库为空,则返回空字符串
RENAME oldname newname更改key的名字,新键如果存在将被覆盖
RENAMENX oldname newname 更改key的名字,如果名字存在则更改失败
DBSIZE返回当前数据库的key的总数
EXPIRE设置某个key的过期时间(秒),(EXPIRE bruce 1000:设置bruce这个key1000秒后系统自动删除)注意:如果在还没有过期的时候,对值进行了改变,那么那个值会被清除。
TTL查找某个key还有多长时间过期,返回时间秒
SELECT index 选择数据库
MOVE key dbindex 将指定键从当前数据库移到目标数据库 dbindex。成功返回 1;否则返回0(源数据库不存在key或目标数据库已存在同名key);
FLUSHDB 清空当前数据库中的所有键
FLUSHALL 清空所有数据库中的所有键

处理字符串的命令
SET key value 给一个键设置字符串值。SET keyname datalength data (SET bruce 10 paitoubing:保存key为burce,字符串长度为10的一个字符串paitoubing到数据库),data最大不可超过1G。
GET key获取某个key 的value值。如key不存在,则返回字符串“nil”;如key的值不为字符串类型,则返回一个错误。

GETSET key value可以理解成获得的key的值然后SET这个值,更加方便的操作 (SET bruce 10 paitoubing,这个时候需要修改bruce变成1234567890并获取这个以前的数据paitoubing,GETSET bruce 10 1234567890)
MGET key1 key2 … keyN 一次性返回多个键的值

SETNX key value SETNX与SET的区别是SET可以创建与更新key的value,而SETNX是如果key不存在,则创建key与value数据
MSET key1 value1 key2 value2 … keyN valueN 在一次原子操作下一次性设置多个键和值
MSETNX key1 value1 key2 value2 … keyN valueN 在一次原子操作下一次性设置多个键和值(目标键不存在情况下,如果有一个以上的key已存在,则失败)
INCR key 自增键值
INCRBY key integer 令键值自增指定数值
DECR key 自减键值
DECRBY key integer 令键值自减指定数值

处理 lists 的命令
RPUSH key value 从 List 尾部添加一个元素(如序列不存在,则先创建,如已存在同名Key而非序列,则返回错误)
LPUSH key value 从 List 头部添加一个元素
LLEN key 返回一个 List 的长度
LRANGE key start end从自定的范围内返回序列的元素 (LRANGE testlist 0 2;返回序列testlist前0 1 2元素)
LTRIM key start end修剪某个范围之外的数据 (LTRIM testlist 0 2;保留0 1 2元素,其余的删除)
LINDEX key index返回某个位置的序列值(LINDEX testlist 0;返回序列testlist位置为0的元素)
LSET key index value更新某个位置元素的值
LREM key count value 从 List 的头部(count正数)或尾部(count负数)删除一定数量(count)匹配value的元素,返回删除的元素数量。
LPOP key 弹出 List 的第一个元素
RPOP key 弹出 List 的最后一个元素
RPOPLPUSH srckey dstkey 弹出 _srckey_ 中最后一个元素并将其压入 _dstkey_头部,key不存在或序列为空则返回“nil”

处理集合(sets)的命令(有索引无序序列)
SADD key member增加元素到SETS序列,如果元素(membe)不存在则添加成功 1,否则失败 0;(SADD testlist 3 /n one)
SREM key member 删除SETS序列的某个元素,如果元素不存在则失败0,否则成功 1(SREM testlist 3 /N one)
SPOP key 从集合中随机弹出一个成员
SMOVE srckey dstkey member 把一个SETS序列的某个元素 移动到 另外一个SETS序列 (SMOVE testlist test 3/n two;从序列testlist移动元素two到 test中,testlist中将不存在two元素)
SCARD key 统计某个SETS的序列的元素数量
SISMEMBER key member 获知指定成员是否存在于集合中
SINTER key1 key2 … keyN 返回 key1, key2, …, keyN 中的交集
SINTERSTORE dstkey key1 key2 … keyN 将 key1, key2, …, keyN 中的交集存入 dstkey
SUNION key1 key2 … keyN 返回 key1, key2, …, keyN 的并集
SUNIONSTORE dstkey key1 key2 … keyN 将 key1, key2, …, keyN 的并集存入 dstkey
SDIFF key1 key2 … keyN 依据 key2, …, keyN 求 key1 的差集。官方例子:
key1 = x,a,b,c
key2 = c
key3 = a,d
SDIFF key1,key2,key3 => x,b
SDIFFSTORE dstkey key1 key2 … keyN 依据 key2, …, keyN 求 key1 的差集并存入 dstkey
SMEMBERS key 返回某个序列的所有元素
SRANDMEMBER key 随机返回某个序列的元素

处理有序集合(sorted sets)的命令 (zsets)
ZADD key score member 添加指定成员到有序集合中,如果目标存在则更新score(分值,排序用)
ZREM key member 从有序集合删除指定成员
ZINCRBY key increment member 如果成员存在则将其增加_increment_,否则将设置一个score为_increment_的成员
ZRANGE key start end 返回升序排序后的指定范围的成员
ZREVRANGE key start end 返回降序排序后的指定范围的成员
ZRANGEBYSCORE key min max 返回所有符合score >= min和score <= max的成员 ZCARD key 返回有序集合的元素数量 ZSCORE key element 返回指定成员的SCORE值 ZREMRANGEBYSCORE key min max 删除符合 score >= min 和 score <= max 条件的所有成员。

使用体会:
个人在觉得redis速度是不用说了(很快的),但是很消耗物理内存,算是redis的一个弊端吧,redis适合数据量比较小速度更新快的类型的网站,比如社区,不适合数据比较庞大的网站,比如论坛。以前用redis应用的一个论坛帖子上,但是因为数据量太大,消耗物理内存惊人而放弃了用 redis!

Let’s talk about TTSERVER
Tokyo Cabinet is an implementation of DBM. The database here consists of a series of records of key-value pairs. 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 the same value for two keys cannot be stored. The following access methods are provided: provide key and value parameters for storage, delete records by key, and read records by key. In addition, traversing keys is also supported, although the order is arbitrary and cannot be guaranteed. These methods are the same as Unix standard DBM, such as GDBM, NDBM, etc., but their performance is much better than them (and therefore can replace them). When stored as a B+ tree, records with the same key can also be stored . Read, store, and delete functions like hash tables are also provided. Records are stored according to user-supplied comparison functions. You can use a sequential or reverse cursor to read each record. According to this principle, forward string matching search and integer interval search are also implemented. In addition, B+ tree transactions are also available. For fixed-length arrays, records are stored labeled by natural numbers. Two or more records with the same key cannot be stored. In addition, the length of each record is limited. The reading method is the same as that of the hash table. Tokyo Cabinet is written in C and provides APIs for c, perl, ruby, and java. Tokyo Cabinet is available on both POSIX and C99 platforms and is released under the GNU Lesser Public License.

tokyocabinet:A key-value DBM database, but does not provide a network interface, hereafter referred to as TC.
tokyotyrant: is a network interface written for TC. It supports the memcache protocol and can also be operated through HTTP, hereafter referred to as TT.

Performance:
Tokyo Cabinet is a DBM database developed by Japanese Mikio Hirabayashi. Tokyo Cabinet is released based on the GNU Lesser General Public License and is developed in C language. It can run on any platform that supports C99 and POSIX. Compared with ordinary DBM databases, it has the following characteristics: small space, high efficiency, high performance, high reliability, support for multiple development languages ​​(APIs for C, Perl, Ruby, Java, and Lua are now available), and supports 64 operating system. The database reads and writes very quickly. It only takes 0.643 seconds to write 1 million pieces of data in the hash mode, and it only takes 0.773 seconds to read 1 million pieces of data, which is several times faster than DBMs such as Berkeley DB.

Tokyo Tyrant plus Tokyo Cabinet form a distributed persistent storage system that supports high concurrency. For any original Memcached client, Tokyo Tyrant can be regarded as a Memcached, but it The data can be stored persistently. This is the same as Sina's Memcachedb.

Comparison between ttserver and memcache:
ttserver is a database and memcached is a cache. Both save data in the form of and perform any operation through key. ttserver can persist data, while memcached stores all data in memory. Memcached will automatically delete expired data, up to 30 days. When memcached cooperates with some APIs, it can automatically serialize data in and out, and read and deserialize data. ttserver has master-slave replication functions, operation logs, etc., which are completely unique to databases. It is said that memcached is adjusting the overall architecture and will support the plugin mechanism. It will separate the network, event processing, and memory storage. In the future, if you want to do disk-based key-value storage, you can just write a storage engine. The secondary development of memcached has entered a small climax.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327945.htmlTechArticleLet’s talk about redis first. Redis is a key/value storage system similar to memcached. It supports relatively many value types for storage. , including string (string), list (linked list), set (set) and zset (ordered set...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn