Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use key/value databases redis and TTSERVER

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use the key/value database redis and TTSERVER, I hope you will learn something after reading this article, let's discuss it together!

Let's start with redis.

Redis is a memcached-like key/value storage system that supports a relatively large number of value types, including string (string), list (linked list), set (collection), and zset (ordered collection). On this basis, redis supports a variety of different sorting methods. Like memcached, data is cached in memory for efficiency. The difference is that redis periodically writes updated data to disk or changes to an appended record file (which I think redis is safer than memcache in data storage), and implements master- slave (master-slave) synchronization on this basis.

The access performance of redis is very high, with 110000 SET operations per second and 81000 GET operations per second (cool speed! ).

Redis provides different commands for different storage types of objects.

Redis currently provides four data types: string,list,set and zset (sorted set).

String is the simplest type, which you can understand as the exact same type as Memcached, one key corresponds to one value, and the operations supported on it are similar to those of Memcached. But it has more functions.

List is a linked list structure, and its main functions are push, pop, getting all values in a range, and so on. The key in the operation is understood as the name of the linked list.

Set is a set, which is similar to the concept of set in our mathematics, such as adding and deleting elements, intersection and difference of multiple sets and so on. The key in the operation is understood as the name of the collection.

Zset is an upgraded version of set, which adds a sequence attribute to set, which can be specified when you add modified elements, and after each assignment, zset will automatically re-adjust the order to the new value. You can understand the mysql table with two columns, one column value and one column storage order. In the operation, key is understood as the name of zset.

The redis command is provided below:

Commands suitable for all types

EXISTS key determines whether a key exists; existence returns 1; otherwise, it 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: does not exist, string: character, list,set,zset,hash)

KEYS pattern returns a list of matching key (KEYS foo*: looks for keys at the beginning of foo)

RANDOMKEY randomly gets an existing key, and returns an empty string if the current database is empty

RENAME oldname newname changes the name of key. If the new key exists, it will be overwritten.

RENAMENX oldname newname changes the name of key. If the name exists, the change fails

DBSIZE returns the total number of key in the current database

EXPIRE sets the expiration time (seconds) of a key (EXPIRE bruce 1000: automatically deletes the system after setting the key1000 of bruce) Note: if you change the value before it expires, then that value will be cleared.

TTL looks up how long it will take for a key to expire. It returns time in seconds.

SELECT index Select Database

MOVE key dbindex moves the specified key from the current database to the target database dbindex. 1 is returned successfully; otherwise 0 is returned (key does not exist in the source database or key with the same name already exists in the target database)

FLUSHDB clears all keys in the current database

FLUSHALL clears all keys in all databases

Commands for working with strings

SET key value sets a string value for a key. SET keyname datalength data (SET bruce 10 paitoubing: save a string paitoubing with a length of 10 to the database with key as burce), and the maximum data should not exceed 1G.

GET key gets the value of a key. The string "nil" is returned if key does not exist, or an error is returned if the value of key is not of type string.

GETSET key value can be understood as the obtained value of key, and then SET this value, more convenient operation (SET bruce 10 paitoubing, this time you need to modify the bruce to 1234567890 and get the previous data paitoubing,GETSET bruce 10 1234567890)

MGET key1 key2... KeyN returns the values of multiple keys at one time

The difference between SETNX key value SETNX and SET is that SET can create and update the value of key, while SETNX creates key and value data if key does not exist.

MSET key1 value1 key2 value2... KeyN valueN sets multiple keys and values at one time under one atomic operation.

MSETNX key1 value1 key2 value2... KeyN valueN sets multiple keys and values at one time in an atomic operation (if the target key does not exist, if more than one key already exists, it fails)

INCR key self-increment key value

INCRBY key integer causes the key value to increase by a specified value.

DECR key self-subtractive key value

DECRBY key integer causes the key value to subtract from the specified value

Processing commands for lists

RPUSH key value adds an element from the tail of the List (create it first if the sequence does not exist, or return an error if the Key with the same name already exists instead of the sequence)

LPUSH key value adds an element from the List header

LLEN key returns the length of a List

LRANGE key start end returns the elements of the sequence from a custom range (LRANGE testlist 0 2; returns the first 0 1 2 elements of the sequence testlist)

LTRIM key start end prunes data outside a range (LTRIM testlist 0 2; retains 0 1 2 elements and deletes the rest)

LINDEX key index returns the sequence value of a location (LINDEX testlist 0; returns the element of the sequence testlist position 0)

LSET key index value updates the value of a location element

LREM key count value removes a certain number (count) of elements matching value from the head (positive count) or tail (negative count) of the List, and returns the number of elements deleted.

LPOP key pops up the first element of List

RPOP key pops up the last element of List

RPOPLPUSH srckey dstkey pops up the last element in _ srckey_ and presses it into the header of _ dstkey_. If key does not exist or the sequence is empty, "nil" is returned.

Commands for handling collections (sets) (indexed unordered sequences)

SADD key member adds an element to the SETS sequence. If the element (membe) does not exist, it succeeds in adding 1, otherwise it fails 0; (SADD testlist 3 / n one)

SREM key member deletes an element from the SETS sequence, fails 0 if the element does not exist, otherwise succeeds 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 two; moves the element two from the sequence testlist to the test, there will be no two element in the testlist)

SCARD key counts the number of elements in a sequence of SETS

SISMEMBER key member learns whether the specified member exists in the collection

SINTER key1 key2... KeyN returns key1, key2, … , the intersection in keyN

SINTERSTORE dstkey key1 key2... KeyN will key1, key2, … The intersection in keyN is stored in dstkey

SUNION key1 key2... KeyN returns key1, key2, … , the union of keyN

SUNIONSTORE dstkey key1 key2... KeyN will key1, key2, … The union of keyN is stored in dstkey

SDIFF key1 key2... KeyN according to key2, … , keyN finds the difference set of key1. Official examples:

Key1 = xmena _ a _ c

Key2 = c

Key3 = a ~ d

SDIFF key1,key2,key3 = > XBI b

SDIFFSTORE dstkey key1 key2... KeyN according to key2, … KeyN calculates the difference of key1 and stores it in dstkey

SMEMBERS key returns all elements of a sequence

SRANDMEMBER key randomly returns the elements of a sequence

Commands for dealing with ordered sets (sorted sets) (zsets)

ZADD key score member adds the specified member to the ordered collection, and updates the score if the target exists.

ZREM key member removes the specified member from the ordered collection

ZINCRBY key increment member add a member to _ increment_, if it exists otherwise it will set a member whose score is _ increment_

ZRANGE key start end returns members of the specified range sorted in ascending order

ZREVRANGE key start end returns members of the specified range sorted in descending order

ZRANGEBYSCORE key min max returns all matches score > = min and score = min and score

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report