Getting started and using Redis

Original: Redis quick start and use

Outline

redis nosql is a distributed database support, his data is stored in memory, and the memory can be timed redis data synchronization to disk, that data can be persistent, and he supports more data structures (string than memcached , list list [stacks and queues], set [set], sorted set [ordered set], hash (hash table)).

redis document: http://redisdoc.com/index.html

Redis usage scenarios

  • Login session storage: Store in redis compared with memcached, data is not lost.
  • Top version / counter: for example, some of the show class project, there is often some former anchor ranked number names. There are some articles to read the amount of technology, or Sina microblogging number of points such as praise.
  • As the message queue: redis such as celery is used as an intermediary.
  • The current line number: the number of the current system Online.
  • Some commonly used data cache: Such as our BBS forum, the plate does not change often, but each visit must be obtained from the Home mysql can be cached in the redis, not every request database.
  • The first 200 articles or reviews cache cache: general users visit the website, only the front part of the article or view a comment, you can put in front of 200 articles and corresponding comments cached. Users access to over, you access the database, and after more than 200 articles, put the article before deleting.
  • Friends relationship: friendship redis use microblogging to achieve.
  • Publish and subscribe functions: can be used to make chat software.

Compare the Redis and Memcached

Memcached Redis
Types of Pure in-memory database RAM disk synchronization database
type of data When necessary to define the data type fixed value Do not need
Virtual Memory not support stand by
Expiration Policy stand by stand by
Storing data security not support Data may be synchronized to the dump.db
Disaster Recovery not support You can restore data disk into memory
distributed stand by Master-slave synchronization
Subscriptions and publishing not support stand by

Redis installation and startup in the windows system

  • Download: redis official is not supported by windows operating system, but Microsoft's open source sector will redis ported to the windows, and therefore not in redis Download the official line, but on GitHub: https://github.com/MicrosoftArchive/redis / releases
  • Installation: Click Next to install a meal on it
  • Run: redis into the path of your installation and execution redis-server.exe redis.windows.confcan run
  • Connection: redis and mysql and mongo are the same, it provides a client to connect. Input command redis-cli (provided that redis installation path has been added to the environment variable) can connect to the server redis

Redis installation and startup of the system in ubuntu

  • installation:sudo apt-get install redis-server
  • Uninstall:sudo apt-get purge --auto-remove redis-server
  • Start: redis After installation, the default will automatically start, you can view the following command:ps aux|grep redis
  • If you want to manually start, you can start with the following command:sudo service redis-server start
  • stop:sudo service redis-server stop

Redis server allows access to other machines

  • Want to make other machines to access this machine redis server. So you want to modify redis.conf profile will bind into a bind [your ip address or 0.0.0.0], other machines can access.
  • bind bind the ip address of the machine network card, and not want to ip addresses of other machines connected. If you have multiple network cards, you can bind multiple ip address of the network card. If the amount is bound to 0.0.0.0, it means that other machines can be accessed by all of the machine ip address.

The operation of the Redis

Redis operation can be divided into two ways, first is to use redis-cli, the second is its operation in a programming language, such as: .NET, Python, etc.

Start redis

sudo service redis-server start

Redis-server connection

redis-cli -h [ip] -p [端口]

Add to

set key value Such as:set key articles

The value to the string value associated key. If the key has been held by other values, set command to overwrite the old value, ignoring its type. And the default expiration time is permanent, that never expire.

delete

del key Such as:del articles

Set an expiration time

expire key timeout(单位为秒)

You can also set the value of the time, with a specified expiration time

set key value EX timeout or:setex key timeout value

Check expiration date

ttl key Such as:ttl articles

See all the current key redis

keys *

List of operations

  • Add an element to the left of the list

    lpush key value

    The value of the key value inserted into the list header. If the key does not exist, an empty list will be created and executed lpush operation. When the key is present but not the type of list, an error is returned

  • Add an element to the right of the list

    rpush key value

    The value of the key value is inserted into the tail of the list. If the key does not exist, an empty list will be created and executed RPUSH operation. When the key is present but not the type of list, an error is returned

  • View the list of elements

    lrange key start stop

    Returns a list of key elements within the specified interval, the interval specified in the offset start and stop, if you want to see the first one to the last one on the left is:lrange key 0 -1

  • Removal of elements in the list

    • Remove and return the head of the list of key elements:lpop key
    • Removes and returns the last element of the list:rpop key
    • Removes and returns a list of key central elements: lrem key count valuethe delete key this list, count value with value elements
  • Specify the return of several elements

    lindex key index The return key to this list, the index for the index of this element

  • Gets the number of elements in the list

    llen key Such as:llen languages

    The count value of the parameter, the removal of the list elements is equal to the parameter value. The count value may be the following

    • count> 0: Header search starting from the end of the table, with the value equal to the removal elements, the number of count
    • count <0: start the search from the head end of the table to the table, equal to the value element is removed, the number of the absolute value of the count
    • count = 0: all value equal to the value in the table is removed

Operation set collection

  • Adding elements

    sadd set value1 value2.... Such as:sadd team web app

  • View element

    smembers set Such as:smembers team

  • Remove elements

    rem set member... Such as:srem team web app

  • View the number of elements in the collection

    scard set Such as:scard team1

  • Obtaining a plurality of sets of intersection

    sinter set1 set2 Such as:sinter team1 team2

  • Acquiring a plurality of sets and sets

    sunion set1 set2 Such as:sunion team1 team2

  • Acquiring a plurality of sets of the difference set

    sdiff set1 set2 Such as:sdiff team1 team2

hash hash operation

  • Add a new value

    hset key field value Such as:hset website baidu baidu.com

    The value domain field hash table key is set to value, if the key does not exist, a new hash table is created and HSET operation. If the domain field already exists in the hash table, the old value will be overwritten.

  • Field corresponding to the acquired hash value

    hget key field Such as:hget website baidu

  • Delete field in a field

    hdel key field Such as:hdel website baidu

  • Get all of the field and a hash value

    hgetall key Such as:hgetall website

  • Gets a hash all of the field

    hkeys key Such as:hkeys website

  • Get all the values ​​in a hash

    hvals key Such as:hvals website

  • The existence of a field judge hash

    hexists key field Such as:hexists website baidu

  • Gets the hash key-value pairs in total

hlen field Such as:hlen website

Transactional operations

Redis transaction can execute multiple commands once the transaction has the following characteristics

  • Isolation Operation: All commands are serialized transaction performed sequentially, not be disturbed by other commands
  • Atomic operation: commands in the transaction are either all executed or not executed all
  • Open a transaction: multiafter all commands executed are executed in this transaction
  • The enforcement branch: exec, will be in multiand execoperations to be submitted
  • Cancel the transaction: discard, will multiall cancel the command
  • Monitoring one or more Key: watch key..., a monitor (or more) key, if this (or these) key changes to other orders are executed before the transaction, then the transaction will be interrupted
  • Cancellation monitor all the key:unwatch

Publish / subscribe operations

  • Channel to a news release:publish channel message
  • Subscribe to a news channel:subscribe channel

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11392123.html