NoSQL series: Redis (1)

Source: Internet
Author: User
Tags install redis redis server

NoSQL series: Redis (1)

Redis environment preparation rredis function library rredis basic Use Cases

Each chapter is divided into "text description" and "code section" to ensure consistency between the text description and the code.

Chapter 1 prepare the Redis Environment

Text description:

First, prepare the environment. Here I chose the 64-bit server version of Linux Ubuntu operating system 12.04. You can choose a proper Linux version based on your usage habits.

Redis installation is skipped. Sudo apt-get install redis-server

View the Redis server environment

Run the/etc/init. d/redis-server command to start redis-server. The default port is port = 6379.

On the server side, use telnet to connect to redis-server

Use telnet to insert data and read data

In the R language environment 2.15.0, WinXP remotely connects to the Redis server.

Code Section:

View Operating System

 
 
  1. ~ uname -a      Linux AY121111030241cda8003 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux  ~ cat /etc/issue      Ubuntu 12.04.1 LTS \n \l  

Start redis

 
 
  1. ~ /etc/init.d/redis-server start      Starting redis-server: redis-server.  

View System Processes

 
 
  1. ~ ps -aux|grep redis      redis    20128  0.0  0.0  10676  1428 ?        Ss   16:39   0:00 /usr/bin/redis-server /etc/redis/redis.conf  

View startup logs

 
 
  1. ~ cat  /var/log/redis/redis-server.log       
  2. [20128] 14 Apr 16:39:43 * Server started, Redis version 2.2.12     
  3. [20128] 14 Apr 16:39:43 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.    
  4. [20128] 14 Apr 16:39:43 * The server is now ready to accept connections on port 6379  

Telnet to redis-server

 
 
  1. ~ telnet localhost 6379      Trying 127.0.0.1...     Connected to localhost.     Escape character is '^]'.  

Insert data

 
 
  1. rpush data 1     :1      rpush data 2     :2  

Query data

 
 
  1. lrange data 0 -1     *2     $1     1     $1     2  

R Language Development Environment 2.15.0, WinXP

~ R R version 2.15.0 (2012-03-30) Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-pc-mingw32/i386 (32-bit) 

Chapter 2 rredis function library

Rredis provides the 100 function to operate on redis applications. Although there are many functions, the usage is relatively simple, the R language is flexible enough, and the code is concise.

The following lists all the rredis function libraries. I only select some common introduction.

Text description:

Establish a connection and close the connection

 
 
  1. redisConnect() , redisClose()  

Clear current/All Database Data

 
 
  1. redisFlushDB() , redisFlushAll()  

List all KEY values and number of keys

 
 
  1. redisKeys(), redisDBSize()  

Select to switch Database: 0 is the default database

 
 
  1. redisSelect(0)      

Insert string objects in batches

 
 
  1. redisSet('x',runif(5)), redisMSet(list(x=pi,y=runif(5),z=sqrt(2)))  

Reads string objects in batches

 
 
  1. redisGet('x'), redisMGet(c('x','y','z'))  

Delete object

 
 
  1. redisDelete('x')  

Insert array object on the left and array object on the right

 
 
  1. redisLPush('a',1), redisRPush('a','A')  

An array object is displayed on the left, and an array object is displayed on the right,

 
 
  1. redisLPop('a'), redisRPop('a')  

List of array objects on the left

 
 
  1. redisLRange('a',0,-1)  

Insert a set object

 
 
  1. redisSAdd('A',runif(2))  

Displays the set object with several elements. The List displays the set object elements.

 
 
  1. redisSCard('A'), redisSMembers('A')  

Display the difference set, intersection, and union of two set objects

 
 
  1. redisSDiff(c('A','B')),redisSInter(c('A','B')),redisSUnion(c('A','B'))  

Code Section:

A total of 100 functions

redisAuth redisBgRewriteAOF redisBgSave redisBLPop redisBRPop redisBRPopLPush redisClose redisCmd redisConnect redisDBSize redisDecr redisDecrBy redisDelete redisDiscard redisEval redisExec redisExists redisExpire redisExpireAt redisFlushAll redisFlushDB redisGet redisGetContext redisGetResponse redisGetSet redisHDel redisHExists redisHFields redisHGet redisHGetAll redisHIncrBy redisHKeys redisHLen redisHMGet redisHMSet redisHSet redisHVals redisIncr redisIncrBy redisInfo redisKeys redisLIndex redisLLen redisLPop redisLPush redisLRange redisLRem redisLSet redisLTrim redisMGet redisMonitorChannels redisMove redisMSet redisMulti redisPublish redisRandomKey redisRename redisRPop redisRPopLPush redisRPush redisSAdd redisSave redisSCard redisSDiff redisSDiffStore redisSelect redisSet redisSetBlocking redisSetContext redisShutdown redisSInter redisSInterStore redisSIsMember redisSlaveOf redisSMembers redisSMove redisSort redisSPop redisSRandMember redisSRem redisSubscribe redisSUnion redisSUnionStore redisTTL redisType redisUnsubscribe redisUnwatch redisWatch redisZAdd redisZCard redisZIncrBy redisZInterStore redisZRange redisZRangeByScore redisZRank redisZRem redisZRemRangeByRank redisZRemRangeByScore redisZScore redisZUnionStore 

Chapter 3 basic operations of rredis

Text description:

First, install the rredis class library and load the class library.

RedisConnect (host = "192.168.1.101", port = 6379)

Then, use the redisConnect () function to establish a connection with the Redis Server. If the local connection redisConnect () is not a parameter, the following example uses remote connection and adds the host parameter to configure the IP address. RedisConnect (host = "192.168.1.101", port = 6379)

Basic operations of redis: it is recommended to link to switch the database. The List displays all KEY values, clears the current database data, clears all database data, and closes the link,

String-type operations: insert, read, delete, insert and set expiration time, batch operations

List Operation: insert, read, and pop-up

Set Operations: insert, read, intersection, difference set, and Union

Interaction between rredis and redis-cli

Code Section:

Basic operations of redis:

 
 
  1. # Installing rredis
  2. Install. packages (rredis)
  3.  
  4. # Loading the rredis class library
  5. Library (rredis)
  6.  
  7. # Remote connection to redis server
  8. RedisConnect (host = "192.168.1.101", port = 6379)
  9.  
  10. # Listing all keys
  11. RedisKeys ()
  12. [1] "x" "data"
  13.  
  14. # How many keys are displayed
  15. RedisDBSize ()
  16. [1] 2
  17.  
  18. # Switching database 1
  19. RedisSelect (1)
  20. [1] "OK"
  21. RedisKeys ()
  22. NULL
  23.  
  24. # Switching database 0
  25. RedisSelect (0)
  26. [1] "OK"
  27. RedisKeys ()
  28. [1] "x" "data"
  29.  
  30. # Clearing data in the current database
  31. RedisFlushDB ()
  32. [1] "OK"
  33.  
  34. # Clear all database data
  35. RedisFlushAll ()
  36. [1] "OK"
  37.  
  38. # Close the link
  39. RedisClose ()

String type operation:

 
 
  1. # Insert object
  2. RedisSet ('x', runif (5 ))
  3. 1] "OK"
  4.  
  5. # Reading objects
  6. RedisGet ('x ')
  7. [1] 0.67616159 0.06358643 0.07478021 0.32129140 0.16264615
  8.  
  9. # Set the data expiration time
  10. RedisExpire ('x', 1)
  11. Sys. sleep (1)
  12. RedisGet ('x ')
  13. NULL
  14.  
  15. # Batch insert
  16. RedisMSet (list (x = pi, y = runif (5), z = sqrt (2 )))
  17. [1] TRUE
  18.  
  19. # Batch read
  20. RedisMGet (c ('x', 'y', 'z '))
  21. $ X
  22. [1] 3.141593
  23. $ Y
  24. [1] 0.9249501 0.3444994 0.6477250 0.1681421 0.2646853
  25. $ Z
  26. [1] 1.414214
  27.  
  28. # Deleting data
  29. RedisDelete ('x ')
  30. [1] 1
  31. RedisGet ('x ')
  32. NULL


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.