Summary of redis use (redis client use) NoSQL

Keywords: Database Redis Oracle MongoDB

Summary of redis use (1) (redis client use) NoSQL
NoSQL
From Baidu Encyclopedia

NoSQL, refers generally to non-relational databases.With the rise of Internet web2.0 websites, traditional relational databases have been unable to cope with web2.0 websites, especially Web 2.0 pure dynamic websites of super-large-scale and highly concurrent SNS type, which exposes a lot of problems that can not be overcome. Non-relational databases have developed rapidly because of their own characteristics.NoSQL databases are created to address the challenges posed by multiple types of data for large data collections, especially for large data applications.

Four categories of NoSQL databases

  1. Key-Value storage database

    This type of database mainly uses a hash table that has a specific key and a pointer to specific data.The advantage of the Key/value model for IT systems is that it is simple and easy to deploy.But if the DBA only queries or updates some values, Key/value is inefficient.Examples include Tokyo Cabinet/Tyrant, Redis, Voldemort, Oracle BDB.
  2. Columns store databases.

    This part of the database is usually used to cope with large amounts of data stored in distributed storage.Keys still exist, but they are characterized by pointing to multiple columns.These columns are arranged by the column family.Examples: Cassandra, HBase, Riak.
  3. Document database

    Document databases are inspired by the Lotus Notes office software and are similar to the first key-value store.This type of data model is versioned, and semi-structured documents are stored in specific formats, such as JSON.Document databases can be viewed as an upgraded version of key-value databases, allowing nesting of key values between them.Document databases are more efficient to query than key databases.For example: CouchDB, MongoDb. Document-based database SequoiaDB is also available in China and has been source.
  4. Graph database

    Unlike other row, column, and rigid SQL databases, a graphical database uses a flexible graphics model and can be extended to multiple servers.NoSQL databases do not have a standard query language (SQL), so a data model is required for database queries.Many NoSQL databases have REST-style data interfaces or query API s.For example: Neo4J, InfoGrid, Infinite Graph.

    Therefore, we conclude that NoSQL databases are more applicable in the following situations: 1, the data model is simple; 2, IT systems need more flexibility; 3, database performance requirements are high; 4, high data consistency is not required; 5, for a given key, it is easier to map the environment of complex values.

Introduction to Redis
Redis is completely open source, free of charge, complies with the BSD protocol, is a high-performance key-value database, and is also a NoSQL database.

Redis and other key-value caching products have three features:

Redis supports data persistence, allowing you to save data in memory on disk and reload it for use on reboot.
Redis not only supports simple key-value data, but also provides storage of list, set, zset, hash and other data structures.
Redis supports data backup, which is master-slave mode.
Redis Advantage

  1. Very high performance - Official redis are tested, 50 concurrently execute 100,000 requests, Redis reads 110,000 times per second, and writes 81,000 times per second.
  2. The rich data type redis stores keys based on the key/value format, which we generally think of as the String type. There are five types of values

    • String string
    • Hash hash type is similar to HashMap type in Java
    • List Chain List
    • Set Collection
    • Sorted Set Ordered Set (also known as zset)
  3. Atomic - All operations of Redis are atomic, meaning they either succeed or fail without execution at all.A single operation is atomic.Multiple operations also support transactions, that is, atomicity, wrapped up in MULTI and EXEC directives.
  4. Rich features - Redis also supports publish/subscribe, notification, key expiration, and so on.
    How is Redis different from other key-value stores?
  5. Redis has a more complex data structure and provides atomic operations on them, which is an evolutionary path different from other databases.Redis's data types are based on basic data structures while being transparent to programmers without additional abstraction.
  6. Redis runs in memory but can be persisted to disk, so there is a memory trade-off between high-speed reads and writes to different datasets because the amount of data cannot be larger than hardware memory.Another advantage of in-memory databases is that Redis can do a lot of things with high internal complexity because it is very simple to operate in memory compared to the same complex data structure on disk.At the same time, they are compact and appended in terms of disk formats because they do not require random access.
  7. Use

windows Client Use of redis
Redis commands are rich in command groups including Cluster, Connection, Geo, Hashes, HyperLogLog, Keys, Lists, Pub/Sub, Scripting, Server, Sets, Sorted Sets, Strings, Transactions and a total of 14 redis commands. If you are interested, please visit the redis website to view all redis commands, where we only focus on the common redis commandsAnd commands related to five data types
1. Start the redis-server.exe server side
2. Open the redis-cli.exe client
Common commands for redis (key and value in redis are case sensitive)

Instructions of type String

1. Storage: set key value
        127.0.0.1:6379> set username laowang
        OK
2. Obtain: get key
        127.0.0.1:6379> get username
        "laowang"
3. Delete: del key
        127.0.0.1:6379> del username
        (integer) 1

Instructions of type Hash

1. Storage: hset key field value
        127.0.0.1:6379> hset myhash address beijing
        (integer) 1
        127.0.0.1:6379> hset myhash level one
        (integer) 1
2. Obtain: 
        * hget key field: Gets the specified field Corresponding values
            127.0.0.1:6379> hget myhash level
            "one"
        * hgetall key: Get All field and value
            127.0.0.1:6379> hgetall myhash
            1) "address"
            2) "beijing"
            3) "level"
            4) "one"
3. Delete: hdel key field
        127.0.0.1:6379> hdel myhash level
        (integer) 1

Instructions of type List

1. Add to:
    1. lpush key value: Add elements to the left of the list       
    2. rpush key value: Add elements to the right of the list
        127.0.0.1:6379> lpush myList 1
        (integer) 1
        127.0.0.1:6379> lpush myList 2
        (integer) 2
        127.0.0.1:6379> rpush myList 2
        (integer) 3
        127.0.0.1:6379> rpush myList 3
        (integer) 4
2. Obtain: lrange key start end : Scope acquisition (0 -1 Refers to getting all)
        127.0.0.1:6379> lrange myList 0 -1
        1) "2"
        2) "1"
        3) "2"
        4) "3"
3. Delete:
    1. lpop key:  Delete the leftmost element of the list and return it
    2. rpop key:  Delete the rightmost element of the list and return it
        127.0.0.1:6379> lpop myList
        "2"
        127.0.0.1:6379> rpop myList
        "3"

Set type instruction set type value is unique

1. Storage: sadd key value
        127.0.0.1:6379> sadd myset one
        (integer) 1
        127.0.0.1:6379> sadd myset one
        (integer) 0
        127.0.0.1:6379> sadd myset ONE
        (integer) 1
2. Obtain: smembers key:Obtain set All elements in the collection
        127.0.0.1:6379> smembers myset
        1) "one"
        2) "ONE"
3. Delete: srem key value:delete set An element in a collection  
        127.0.0.1:6379> srem myset ONE
        (integer) 1

Instructions of type Sorted Set

1. Storage: zadd key score value
        127.0.0.1:6379> zadd mysort 666 laotie
        (integer) 1
        127.0.0.1:6379> zadd mysort 66 meimaobing
        (integer) 1
        127.0.0.1:6379> zadd mysort 6 chenduxiu
        (integer) 1
2. Obtain: zrange key start end [withscores]
        127.0.0.1:6379> zrange mysort 0 -1
        1) "chenduxiu"
        2) "meimaobing"
        3) "laotie"

        127.0.0.1:6379> zrange mysort 0 -1 withscores
        1) "chenduxiu"
        2) "6"
        3) "meimaobing"
        4) "66"
        5) "laotie"
        6) "666"
3. Delete: zrem key value
        127.0.0.1:6379> zrem mysort laotie
        (integer) 1

Other commonly used instructions:

1. dbsize : Return to the current database's key Quantity.
    127.0.0.1:6379> dbsize
    (integer) 2
2. Keys pattern: Find all key s that match a given pattern pattern.
    Store values in redis
    127.0.0.1:6379> set One1 redis
    OK
    127.0.0.1:6379> set One2 mysql
    OK
    127.0.0.1:6379> set One3 oracle
    OK
    127.0.0.1:6379> set One4 mongodb
    OK
    127.0.0.1:6379> keys One*
    1) "One3"
    2) "One4"
    3) "One1"
    4) "One2"
    127.0.0.1:6379> keys * (this directive is not recommended)
    1) "myhash"
    2) "One3"
    3) "One4"
    4) "One1"
    5) "One2"
    6) "mysort"
3. del key [key …] : Delete one or more given key,And corresponding value,Nonexistent key Will be ignored, this directive can delete any data type   
    127.0.0.1:6379> del One1 One2
    (integer) 2

4. type key : Return key The type of value stored.
    127.0.0.1:6379> type mysort
    zset
    127.0.0.1:6379> type myhash
    hash
    127.0.0.1:6379> type One3
    string

5. exists key : Check Specified key Is there any
    127.0.0.1:6379> exists hehe
    (integer) 0
    127.0.0.1:6379> exists myhash
    (integer) 1

6. expire key seconds: For a given key Set the lifetime when key Outdated(Lifetime 0 ),It will be deleted automatically. seconds The unit is in seconds.
    127.0.0.1:6379> expire One3 20
    (integer) 1
    127.0.0.1:6379> get One3
    "oracle"
    127.0.0.1:6379> get One3
    "oracle"
    127.0.0.1:6379> get One3
    "oracle"
    127.0.0.1:6379> get One3
    (nil)

7. ttl key  Returns the given value in seconds key Remaining lifetime of(TTL, time to live).  
   //Returns -2 when the key does not exist. 
   //Returns -1 when the key exists but does not set the remaining lifetime. 
   //Otherwise, in seconds, the key's remaining lifetime is returned.

    127.0.0.1:6379> set myTtl myRedis
    OK
    127.0.0.1:6379> set myTtl2 myRedis2
    OK
    127.0.0.1:6379> expire myTtl 30
    (integer) 1
    127.0.0.1:6379> ttl myTtl
    (integer) 20
    127.0.0.1:6379> ttl myRedis
    (integer) -2
    127.0.0.1:6379> ttl myTtl2
    (integer) -1

8. flushall: Empty the whole Redis Server data(Delete all databases key ),This command never fails.
   flushdb : Empty all in the current database key,This command never fails.
    127.0.0.1:6379> flushdb
    OK
    127.0.0.1:6379> flushall
    OK
    127.0.0.1:6379> keys *
    (empty list or set)

Original Address https://www.cnblogs.com/cpq1995/p/10734205.html

Posted by aaronxbond on Wed, 08 May 2019 23:12:38 -0700