Introduction Redis cache database

Cache database Introduction

redis operation

1, cache data presentation

NoSQL (NoSQL = Not Only SQL), which means "not only SQL", refers to non-relational database, with the rise of the Internet web, traditional relational databases in dealing with web sites, particularly large scale and high concurrent SNS type of web2.0 pure dynamic site has appeared to be inadequate too exposed a lot of problems difficult to overcome, rather than relational databases due to its own characteristics has been very rapid development. NoSQL database to generate massive data sets to solve the challenges of multiple types of data, specifically the problem is big data applications

Four categories of NoSQL databases

Four classification NoSQL databases: Key (Key-Value) stored in the database (Redis), a column store database, document database, graphics (Graph) database,

  1. Category: key (key-value)
  2. 举例:Tokyo,Cabinet/Tyrant,Redis,Voldemort,Oracle BDB
  3. Typical application scenarios: content caching, mainly for high-load processing to access large amounts of data, but also for some of the logging system, and so on.
  4. Data Model: Key Value of the key points, usually hash
  5. Pros: Fast Find
  6. Disadvantages: unstructured data, usually only be used as string or binary data

redis Introduction

Why is redis key / value pairs, because the k, v faster, k, v bottom of the first turn and then use the hash lookup using the dichotomy 2

redis is one of the key-value nosql database industry mainstream. And Memcached Similarly, it supports relatively more stored value type pair comprises a string (string), List (list), SET (set), zset (sorted set - ordered set) and hash (hash type). These data types are supported push / pop, add / remove and on the intersection and union, and difference richer operation, and these operations are atomic (non-varying). On this basis, redis support a variety of different ways of sorting. Like with memcached, in order to ensure efficiency, the data is cached in memory, the difference is redis will periodically update the data written to disk or to modify the operating log file additional written, and on this basis to achieve a master -slave (master-slave) synchronization

Redis advantage

  1. Asynchronous Fast: Redis can be performed very fast setting operation about 110,000 per second, 81,000 / read operations per second
  2. Support for rich data types: Redis supports most developers already know most lists, sets, sortable collection, hash and other data types
  3. Operations are atomic: All Resis operations are atomic, ensuring that when two clients simultaneously access Redis server get is the value (latest value) updated
  4. MultiUtility tool: Redis is a versatile utility that can be many such as: any Web application sessions, web page hits such as: caching, messaging queue using (Redis native support publish / subscribe), in the application, such as short-term data:

Redis installation environment

To install the Ubuntu Redis, open a terminal, and enter the following: 1.sudo apt-get update 1. sudo apt-get install redis-server

Redis installed start Redis: redis-server

See if redis still running: redis-cli, which will open a Redis prompt, as shown below: 127.0.0.1:6379> in the prompt information: 127.0.0.1 is the IP address of the machine, port 6379 is running Redis server, PING command is now input, as shown below: 127.0.0.1:6379> ping PONG this shows that you have now successfully installed on the computer Redis

sudo netstat -tulnp view port

df: View disk usage

operating

1, String operation

String redis in the memory in accordance with a name corresponding to a stored value

  1. set(name, value, ex=None, px=None, nx=False, xx=False)
set name1 2

在Redis中设置值,默认,不存在则创建,存在则修改
参数:
     ex,过期时间(秒)
     px,过期时间(毫秒)
     nx,如果设置为True,则只有name不存在时,当前set操作才执行
     xx,如果设置为True,则只有name存在时,岗前set操作才执行
  1. setnx(name, value)
    127.0.0.1:6379> setnx name1 3
    (integer) 0
    127.0.0.1:6379> setnx name2 4
    (integer) 1
    
    设置值,只有name不存在时,执行设置操作(添加)
  1. setex(name, value, time)
    127.0.0.1:6379> setex name3 3 3
            OK
    127.0.0.1:6379> get name3
    (nil)

    # 设置值
    # 参数:
    # time,过期时间(数字秒 或 timedelta对象)
  1. psetex(name, time_ms, value)
    # 设置值
    # 参数:
    # time_ms,过期时间(数字毫秒 或 timedelta对象)
  1. mset(*args, **kwargs)
    批量设置值
    127.0.0.1:6379> mset k 2 k1 3
    OK
  1. get(name)
    获取值
    127.0.0.1:6379> get k
    "2"
  1. mget(keys, *args)
    批量获取
    127.0.0.1:6379> mget k k1
    1) "2"
    2) "3"
    
    获取所有的key:
    127.0.0.1:6379> Keys *
    1) "name2"
    2) "k"
    3) "k1"
    4) "name1"
  1. getrange (key, start, end)
    # 获取子序列(根据字节获取,非字符)
    # 参数:
    # name,Redis 的 name
    # start,起始位置(字节)
    # end,结束位置(字节)
    127.0.0.1:6379> set name4 keke
    OK
    127.0.0.1:6379> getrange name4 0 2
    "kek"
    127.0.0.1:6379> 
    
  1. setrange(name, offset, value)
    # 修改字符串内容,从指定字符串索引开始向后替换(新值太长时,则向后添加)
    # 参数:
    # offset,字符串的索引,字节(一个汉字三个字节)
    # value,要设置的值
    
    127.0.0.1:6379> setrange name4 2 yu
    (integer) 4
    127.0.0.1:6379> get name4
    "keyu"
  1. setbit(name, offset, value)
    # 对name对应值的二进制表示的位进行操作
 
    # 参数:
    # name,redis的name
    # offset,位的索引(将值变换成二进制后再进行索引)
    # value,值只能是 1 或 0
    找出50乙登陆的用户:
        用一个key=LOGIN_USERS存储用户登陆,从位数0开始,
        setbit Login_USERS 10 1 ,10是一个用户的唯一usei当作位数,值1表示登陆,用bitcount LOGIN_USERS所有为1(登陆的总数),
        用getbit LOGIN_USERS 10 :找出userid为10的用户是否是登陆的
  1. getbit(name, offset)
    # 获取name对应的值的二进制表示中的某位的值 (0或1)
  1. bitcount(key, start=None, end=None)
    # 获取name对应的值的二进制表示中 1 的个数
    # 参数:
    # key,Redis的name
    # start,位起始位置
    # end,位结束位置
  1. strlen(name)
# 返回name对应值的字节长度(一个汉字3个字节)
  1. incr(self, name, amount=1)
    # 自增 name对应的值,当name不存在时,则创建name=amount,否则,则自增。
     
    # 参数:
    # name,Redis的name
    # amount,自增数(必须是整数)
 
# 注:同incrby
  1. incrbyfloat(self, name, amount=1.0)
    # 自增 name对应的值,当name不存在时,则创建name=amount,否则,则自增。
     
    # 参数:
    # name,Redis的name
    # amount,自增数(浮点型)
  1. decr(self, name, amount=1)
    # 自减 name对应的值,当name不存在时,则创建name=amount,否则,则自减。
     
    # 参数:
    # name,Redis的name
    # amount,自减数(整数)
  1. append(key, value)
# 在redis name对应的值后面追加内容
 
# 参数:
    key, redis的name
    value, 要追加的字符串

https://www.cnblogs.com/alex3714/articles/6217453.html

Guess you like

Origin www.cnblogs.com/venvive/p/11696123.html