This time we implemented redis on a remote server and simply used it in a Java project

Introduction to the

Redis is fully open source, BSD compliant, and is a high-performance key-value database.

Redis and other key-value caching products have the following three characteristics:

  • Redis supports data persistence, saving data in memory to disk, which can be reloaded for use upon restart.
  • Redis not only supports simple key-value type data, but also provides the storage of list, set, zset, hash and other data structures.
  • Redis supports data backup, namely, data backup in master-slave mode.

advantage

  • High performance – Redis can read 110,000 times /s and write 81,000 times /s.
  • Rich data types – Redis supports binary case Strings, Lists, Hashes, Sets and Ordered Sets data type operations.
  • Atomic – All operations in Redis are atomic, meaning that they either succeed or fail at all. Individual operations are atomic. Multiple operations also support transactions, namely atomicity, wrapped in MULTI and EXEC instructions.
  • Rich features – Redis also supports publish/subscribe, notifications, key expiration, and more.

The installation

  1. First on the remote server, select the version to install
Wget HTTP: / / http://download.redis.io/releases/redis-5.0.7.tar.gzCopy the code
  1. Extract:
The tar - ZXVF redis - 5.0.7. Tar. GzCopy the code
  1. After decompression, the folder: CD redis-5.0.7 appears, and the directory is entered: CD Redis-5.0.7

  2. Run the make command in the redis-5.0.7 directory to compile

  3. If make is complete, run make install

OK, the installation is successful at this point

Configure the redis.conf file

  1. Change daemonize no to yes to start the service in the background as follows:
daemonize yes
Copy the code

Delete bind 127.0.0.1 from the redis configuration file. This must be commented out or changed to 0.0.0.0, otherwise it will only connect to the local server

Bind 0.0.0.0 or #bind 127.0.0.1Copy the code
  1. Change protected-mode to no, default is yes
protected-mode no
Copy the code

Checking the Firewall of the Server

Check whether the server firewall is open port 6379:

Firewall-cmd --query-port=6379/ TCP You can skip firewall-cmd --zone=public --add-port=6379/ TCP --permanent # then restart firewall-cmd --reloadCopy the code

The back-end

Python USES

Download:

pip install redis
Copy the code

The instance

import redis   # import redis module

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set('name'.'runoob')  # set the value of name
print(r['name'])
print(r.get('name'))  Select name from key
print(type(r.get('name')))  # check type
Copy the code

Results:

runoob
runoob
<class 'str'>
Copy the code

String operations

set

# set the value in Redis, create it if it does not exist, modify it if it does exist
r.set('name'.'zhangsan')
"' parameters: Set (name, value, ex=None, px=None, nx=False, xx=False) ex, expiration time (s) px, expiration time (ms) nx, if set to True, name does not exist, Setnx (name, value) xx; if True, the set operation is performed only if name existsCopy the code


setex(name, value, time)
# Set expiration time (s)

psetex(name, time_ms, value)
# Set expiration time (seconds)

Copy the code
# Batch set values
r.mset(name1='zhangsan', name2='lisi')
# or
r.mset({"name1":'zhangsan'."name2":'lisi'})


Copy the code

get

# # # get values (name) in bulk for print (r.m get (" name1 ", "name2")) # or li = (" name1 ", "name2") print (r.m get (li))Copy the code

exists(name)

Checks whether key exists and returns 0 or 1

print(r.exists('name'))
Copy the code

Program source code