redis installation and use

First, apply the words of others:

redis cattle cross, can different types of data stored in the memory, stored into memory when removed fast. So, he was very popular. There is also a very cattle fork called memcache, but he saved data type is very limited, only into string type. So, instead of memcache with redis more and more.


Two, windows version is installed

More general development and debugging on the windows, so this version or to install drop ~.
1. Download

Address: https: //github.com/dmajkic/redis/downloads. Or network disk (http://l3.yunpan.cn/lk/sVEUY9ygDJIdD)(redis-2.4.5-win32-win64.zip)

Situation of their own system, select the version installed in accordance with, I chose the latest version, 64.

Once downloaded, unzip, there are two folders, a 32, a 64. My system is 64-bit windows, so I unpacked 64-bit.


2. Install

redis installation and memcache like, simpler than him.

64 downloaded folder subfolders. Copied to the D: \ under redis. On it. It's that simple.


3. Run

Open cmd. Enter D: \ redis

Run: redis-server.exe redis.conf

C:\Users\yi.yang>d:

D: \> cd repeat

D:\redis>redis-server.exe redis.conf
[5556] 26 Nov 14:47:55 * Server started, Redis version 2.4.5
[5556] 26 Nov 14:47:55 # Open data file dump.rdb: No such file or directory
[5556] 26 Nov 14:47:55 * The server is now ready to accept connections on port 6379
[5556] 26 Nov 14:47:56 - 0 clients connected (0 slaves), 1179896 bytes in use
[5556] 26 Nov 14:48:01 - 0 clients connected (0 slaves), 1179896 bytes in use


Then open another cmd window. Enter D: \ redis

Run: redis-cli.exe -h 127.0.0.1 -p 6379

C:\Users\yi.yang>d:

D: \> cd repeat

D:\redis>redis-cli.exe -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set yangyi 100
OK
redis 127.0.0.1:6379> get yangyi
"100"
redis 127.0.0.1:6379>


Perform simple set and get commands, data, indicating a successful installation.


Third, the basic use of redis

redis is a key-value storage system. And Memcached Similarly, it supports relatively more stored value type, comprising a string (string), List (list), SET (set) and zset (ordered set).
1. String easy access operation

set set value of the element;

get the value of the element removed;

increment incr element 1;

the self-energizing elements incrby n


D:\redis>redis-cli.exe -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set yangyi 100
OK
redis 127.0.0.1:6379> get yangyi
"100"
redis 127.0.0.1:6379> set name yangyi
OK
redis 127.0.0.1:6379> get name
"yangyi"
redis 127.0.0.1:6379> incr name
(error) ERR value is not an integer or out of range
redis 127.0.0.1:6379> set sum 12
OK
redis 127.0.0.1:6379> ince sum
(error) ERR unknown command 'ince'
redis 127.0.0.1:6379> incr sum
(integer) 13
redis 127.0.0.1:6379> get sum
"13"
redis 127.0.0.1:6379> incrby sum 5
(integer) 18
redis 127.0.0.1:6379> set blog:url "http://yangyi.com"
OK
redis 127.0.0.1:6379> get blog
(nil)
redis 127.0.0.1:6379> get blog:url
"http://yangyi.com"
redis 127.0.0.1:6379>

 

2. List list of operations

rpush tail added elements;

lpush head element added;

llen number of elements in the list;

rpop pop trailing elements;

lpop pop head element;

lrange user 0 -1 acquiring all the elements;

lrange users 1 2 acquires the first to the second element


redis 127.0.0.1:6379> rpush users "yangyi"
(integer) 1
redis 127.0.0.1:6379> rpush users "zhangsan"
(integer) 2
redis 127.0.0.1:6379> lpush users "lisi"
(integer) 3
redis 127.0.0.1:6379> lrange users 0 -1
1) "lisi"
2) "yangyi"
3) "zhangsan"
redis 127.0.0.1:6379> llen users
(integer) 3
redis 127.0.0.1:6379> rpop users
"zhangsan"
redis 127.0.0.1:6379> llen users
(integer) 2
redis 127.0.0.1:6379> lpop users
"lisi"
redis 127.0.0.1:6379> llen users
(integer) 1
redis 127.0.0.1:6379> lrange users 0 1
1) "yangyi"
repeat 127.0.0.1:6379>


3. Set operation set

sadd add elements to the collection;

smembers view the collection of all the elements;

srem delete the specified element;

sismember view element exists, indicates the presence of 1, 0 does not exist;

The combined set of two set sunion

redis 127.0.0.1:6379> sadd lan 'php'
(integer) 1
redis 127.0.0.1:6379> sadd code 'php'
(integer) 1
redis 127.0.0.1:6379> sadd code 'java'
(integer) 1
redis 127.0.0.1:6379> sadd code 'c++'
(integer) 1
redis 127.0.0.1:6379> smembers code
1) "php"
2) "c++"
3) "java"
redis 127.0.0.1:6379> srem code 'c++'
(integer) 1
redis 127.0.0.1:6379> sismember code 'php'
(integer) 1
redis 127.0.0.1:6379> sadd book 'php'
(integer) 1
redis 127.0.0.1:6379> sadd book 'asp'
(integer) 1
redis 127.0.0.1:6379> sunion code book
1) "asp"
2) "php"
3) "java"
redis 127.0.0.1:6379>


4. Zset ordered set (Sorted Set) operation

1983 add an element zadd students "yangyi" yangyi collection to students inside, and set its birth year is 1989, is used as the score recorded, sorted according to score;

zrange students 0 -1 View All

View zrange students 1 2 1 through 2 elements

redis 127.0.0.1:6379> zadd students 1989 'yangyi'
(integer) 1
redis 127.0.0.1:6379> zadd students 1990 'yuanyuan'
(integer) 1
redis 127.0.0.1:6379> zrange students 0 -1
1) "yangyi"
2) "yuanyuan"
redis 127.0.0.1:6379> zrange students 1 2
1) "yuanyuan"
redis 127.0.0.1:6379>


Four, linux on redis installation.

In fact, on the linux installation easier.


1. Open redis official website http://redis.io/download download page. There downloaded version, there are steps to download and install, very clear.

(1) download, download progress bar will prompt the download is complete in the / home / yangyi / directory.

@ yangyi yangyi: / $ cd / Home / yangyi /
yangyi yangyi @: ~ $ sudo wget http://redis.googlecode.com/files/redis-2.6.5.tar.gz

 

(2) Unpack and install, tar xzf decompression, the directory entry after decompression, make. Can be installed

@ yangyi yangyi: $ ~ Redis the xzf the tar-2.6.5.tar.gz
yangyi yangyi @: ~ $ 2.6.5 CD-Redis /
yangyi yangyi @: ~ / $ Redis the make-2.6.5


To suggest that the official website did not make install. The best make install it. Because of this it. The system will redis several core file, copied to / usr / local / bin / directory. such. Later you can start anywhere and use redis, and without having to go into the installation directory redis in order to start it.

yangyi @ yangyi: ~ / redis-2.6.5 $ make install

 

(3) run, execute redis-server command. This boot is running in the background. Unlike windows cmd very boring, must be open, not closed.

yangyi @ yangyi: ~ / redis-2.6.5 $ redis-server


(4) use to see whether the already running. This time because redis has started, it can be anywhere, any directory using the redis. Execution: redis-cli you can use the redis.

yangyi@yangyi:/$ redis-cli
redis> get foo
"bar"
redis> exit
yangyi@yangyi:/$ cd /
yangyi@yangyi:/$ redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
redis>

show. redis been successfully installed and ready for use. (* ^ __ ^ *)


(4) Let redis boot from the start. windows of the boot from the start very boring, very easy on the linux.

sudo gedit /etc/rc.local


Add to

/usr/local/bin/redis-server

 


2. A method is also much simpler. recommend.

direct:

yangyi@yangyi:/usr/local/bin$ sudo apt-get install redis-server

To get. And the configuration files are automatically God horse doing a good job, what are good boot.

Startup / Shutdown

yangyi@yangyi:/usr/local/bin$ sudo /etc/init.d/redis-server stop

yangyi @ yangyi: / usr / local / bin $ sudo /etc/init.d/redis-server start

 

Original link: https: //blog.csdn.net/think2me/article/details/8225660

Guess you like

Origin www.cnblogs.com/isme-zjh/p/11935670.html