redis tutorial

redis 是一个key-value型的nosql数据库。然后就用了下;

Redis is what is called a key-value store, often referred to as a NoSQL database. The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it. We can use the command SET to store the value "fido" at key "server:name":


    SET server:name "fido"

Redis will store our data permanently, so we can later ask "What is the value stored at key server:name?" and Redis will reply with "fido":


    GET server:name => "fido"

Other common operations provided by key-value stores are DEL to delete a given key and associated value, SET-if-not-exists (called SETNX on Redis) that sets a key only if it does not already exist, and INCR to atomically increment a number stored at a given key:


    SET connections 10
    INCR connections => 11
    INCR connections => 12
    DEL connections
    INCR connections => 1

There is something special about INCR. Why do we provide such an operation if we can do it ourself with a bit of code? After all it is as simple as:

x = GET count
x = x + 1
SET count x

The problem is that doing the increment in this way will only work as long as there is a single client using the key. See what happens if two clients are accessing this key at the same time:

  1. Client A reads count as 10.
  2. Client B reads count as 10.
  3. Client A increments 10 and sets count to 11.
  4. Client B increments 10 and sets count to 11.

We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this way is not an atomic operation. Calling theINCR command in Redis will prevent this from happening, because it is an atomic operation. Redis provides many of these atomic operations on different types of data.


Redis can be told that a key should only exist for a certain length of time. This is accomplished with the EXPIRE and TTL commands.


    SET resource:lock "Redis Demo"
    EXPIRE resource:lock 120

This causes the key resource:lock to be deleted in 120 seconds. You can test how long a key will exist for with the TTL command. It returns the number of seconds until it will be deleted.


    TTL resource:lock => 113
    TTL count => -1

The -1 for the TTL of the key count means that it will never expire. Note that if you SET a key, its TTL will reset.


    SET resource:lock "Redis Demo 1"
    EXPIRE resource:lock 120
    TTL resource:lock => 119
    SET resource:lock "Redis Demo 2"
    TTL resource:lock => -1

Redis also supports several more complex data structures. The first one we'll look at is a list. A list is a series of ordered values. Some of the important commands for interacting with lists are RPUSHLPUSHLLENLRANGELPOP, and RPOP. You can immediately begin working with a key as a list, as long as it doesn't already exist as a different type.

RPUSH puts the new value at the end of the list.


    RPUSH friends "Tom"
    RPUSH friends "Bob"

LPUSH puts the new value at the start of the list.


    LPUSH friends "Sam"

LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve all elements in the list.


    LRANGE friends 0 -1 => ["Sam","Tom","Bob"]
    LRANGE friends 0 1 => ["Sam","Tom"]
    LRANGE friends 1 2 => ["Tom","Bob"]

LLEN returns the current length of the list.


    LLEN friends => 3

LPOP removes the first element from the list and returns it.


    LPOP friends => "Sam"

RPOP removes the last element from the list and returns it.


    RPOP friends => "Bob"

Note that the list now only has one element:


    LLEN friends => 1
    LRANGE friends 0 -1 => ["Tom"]

The next data structure that we'll look at is a set. A set is similar to a list, except it does not have a specific order and each element may only appear once. Some of the important commands in working with sets are SADDSREMSISMEMBERSMEMBERS and SUNION.

SADD adds the given value to the set.

    SADD superpowers "flight"
    SADD superpowers "x-ray vision"
    SADD superpowers "reflexes"

SREM removes the given value from the set.

    SREM superpowers "reflexes"

SISMEMBER tests if the given value is in the set.

    SISMEMBER superpowers "flight" => true
    SISMEMBER superpowers "reflexes" => false

SMEMBERS returns a list of all the members of this set.

    SMEMBERS superpowers => ["flight","x-ray vision"]

SUNION combines two or more sets and returns the list of all elements.

    SADD birdpowers "pecking"
    SADD birdpowers "flight"
    SUNION superpowers birdpowers => ["flight","x-ray vision","pecking"]

The last data structure which Redis supports is the sorted set. It is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.

    ZADD hackers 1940 "Alan Kay"
    ZADD hackers 1953 "Richard Stallman"
    ZADD hackers 1965 "Yukihiro Matsumoto"
    ZADD hackers 1916 "Claude Shannon"
    ZADD hackers 1969 "Linus Torvalds"
    ZADD hackers 1912 "Alan Turing"

In these examples, the scores are years of birth and the values are the names of famous hackers.

    ZRANGE hackers 2 4 => ["Alan Kay","Richard Stallman","Yukihiro Matsumoto"]

That wraps up the Try Redis tutorial. Please feel free to goof around with this console as much as you'd like.

Check out the following links to continue learning about Redis.

 

很好的redis使用入门:

http://blog.csdn.net/eric505124021/article/details/6750431  

最后提到三个使用模式:

handling the unique id problem with a string key and the INCR command;

handling a user login via an user:username:id type key;

handling many-to-many relations with sets.


redis安装配置入门

http://blog.csdn.net/eric505124021/article/details/6745594


redis ruby 入门

http://blog.csdn.net/eric505124021/article/details/6777609

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值