This is the first day of my participation in Gwen Challenge
feeling
In the evening, watch the Nuggets and see this activity. After nine days, I won a T-shirt. Today, I will clear an inventory, focusing on the remote construction of Redis (water ~~~).
Tomorrow will start to output some meaningful, such as GitBook static website generator, GitHub Pages deployment, domain name processing, etc. (not to introduce the record), personal renderings are as follows
Some of GitBook’s pain points have been addressed (compilation errors, hot loading), and some great plug-ins will be introduced
Redis is introduced
Redis is an open source, network-enabled, memory-based, distributed, optional persistence key-value pair storage database written in ANSI C. Development of Redis was sponsored by Redis Labs beginning in June 2015, and from May 2013 to June 2015, its development was sponsored by Pivotal. Prior to May 2013, its development was sponsored by VMware. Redis is the most popular key-value pair storage database, according to DB-Engines.com, a monthly ranking website.
The above introduction is quoted from Wikipedia
To put it simply, Redis is a non-relational cache database that exists in memory and has the following characteristics (a brief description)
- Data is stored in memory with extremely high read/write speed (cache)
- The structure of the data store is simple and follows the form of K and V key-value pairs (non-relational)
- The disadvantage is that data in memory is easily lost and not stored in disk
On a daily basis, a blog platform, its likes can be cached using Redis. Maybe, just like, and want to cancel, and the data is not important (easy to change, large volume, small content), you can consider Redis first, not anxious to interact with the disk
There are five common types of data stored in Redis, strings, Hashes, Lists, sets and sorted sets. All of them are stored in the form of K and V and obtain values through keys
Redis installation
For the installation of Redis, you are advised to download it from the official website and then upload it to the Linux server. Of course, Microsoft provides a Windows version of Redis, for learning no impact, you can consider installing
Decompress Redis (tar ZXVF
) in the directory on the Linux server and enter make to complete the compilation. If It’s a good idea to run’make test’ is displayed, this step is completed. Yum -y install gcc-C ++ automark autoconf
Next, specify the Redis installation directory (make PREFIX=/home/Redis install). After the installation is complete, the bin directory exists under the specified default, which is the actual file to use. Go to the bin directory and run redis through./redis-server
At this point, Redis Linux installation is successful, divided into three steps: upload decompression, compile, install
For the startup of Redis, you can change it to background startup through the configuration file. Copy the redis.conf file from the compilation directory to the bin directory.
Edit the redis.conf file (vim redis.conf) and press I to enter editing mode. Change the configuration item daemonize no to daemonize yes. Press Esc and enter :wq to save the configuration and exit. Of course, you can also directly download the file to Windows local modification
In this case, run the./redis-server redis.conf directory. Enter./ Redis -cli to go to the built-in client. If the front name is 127.0.0.1:xxxx >, the client is successfully started. If the prefix name is not Connected >, the service is not started
/redis-server –daemonize yes to start redis in the background without modifying the redis-conf file. In most cases, the former is recommended
Here, recommend a Redis Windows client operation tool RedisClient, image download address, this GUI tool support Chinese, and free, no need to crack
For remote connections, two configurations need to be modified. Protected-mode no, disable the protection mode. Bind 127.0.0.1 -::1, comment out, all remote access. When connecting to remote Redis, there is no password by default. You can directly enter the host IP address and port number
You can also set and change passwords in the redis. Conf file. For example, requirepass 123456. If requirePass is found, the default value is the password
You need to enter the password after changing itauto <Password>
NOAUTH Authentication Required. prompt
For the above RedisClient, the use of slow, poor experience. It is recommended to name Redis by hand in the terminal, most of the Redis tools for charging
If the Remote Linux connection is interrupted due to Redis, enter a value on the CLIconfig set save ""
,config set appendfsync no
Redis type
Redis command line operations can be performed on Linux terminals or Windows clients. Here, the five basic types of Redis operations are described
Strings type
Strings. A Key corresponds to a Value that exists as a string
- Add data:
set <K> <V>
- Fetching data:
get <K>
- Batch add:
mset <K1> <V1> <K2> <V2>
- Batch take out:
mget <K1> <K2>
- Append data (if no, create a new one) :
append <Key> <Value>
- Reset data (not present, null) :
getset <K> <V>
- Reduced integer (default 1) :
desc <K>
- Reduced integer (arbitrary) :
descby <K> <V>
- Amplified integer (default 1) :
incr <K>
- Amplified integer (any) :
incrby <K> <V>
Here are Strings reads and writes, either individually or in batches, and the subsequent reads and writes are similar. It is worth noting that the reduction and amplification operations are essentially atomic operations, and there is no thread safety problem under concurrent operations
Lists the type
Corresponding to a linked list structure, an ordered collection of string elements
- Left add data:
lpush <K> <Value1> <Value2> <Value3>
- Add data right:
rpush <K> <Value1> <Value2> <Value3>
- Fetching data (index) :
lindex <Key> <index>
- Fetch data (range) :
lrange <Key> <Start> <Stop>
- Total data length:
llen <Key>
- Delete data (from left to right) :
lrem <Key> <Number> <Value>
For lists types, you can add data (queues) from left and right, which is important to note. When fetching data, you can delete data by index or range, deleting several duplicate data
Sets the type
Stores unordered collections with non-repeatable elements, overridden by default
- Add data:
sadd <Key> <Value1> <Value2>
- Find data:
smembers <Key>
- Get length:
scard <Key>
- Delete data:
srem <Key> <Value>
Note that the sets type in Redis, where duplicate elements cannot be stored, can refer to Java’s set type. Simply put, you can’t ask for a fixed range of elements
Sorted sets type
A collection of ordered storage, accomplished by specifying an index of elements. Elements cannot be repeated
- Add data:
zadd <Key> <index1> <Value1> <index2> <Value2> <index3> <Value3>
- Query data:
zrange <Key> <Start> <Stop>
- Get length:
zcard <Key>
- Delete data:
zrem <Key> <Value1> <Value2>
For the sorted sets type, when inserting data, the storage can be ordered by assigning indexes
Hashs type
For hashS type, it is more complicated. It actually stores a K, where Filed, Value, Field and Value are strings. You can refer to the storage structure of HashMap
- Add data:
hset <K> <Filed> <Value>
,hset <K> <Filed1> <Value1> <Filed2> <Value2>
- Get data:
hget <K> <Filed>
- Batch add:
hmset <K> <Filed1> <Value1> <Filed2> <Value2>
- Batch take out:
hmget <K> <Filed1> <Filed2> <Filed3>
- Take out all:
hgetall <K>
- Delete data:
hdel <K> <Filed>
Although only some common commands are described here, it is important to note. For example, in the process of extraction and deletion, the Value of Filed is actually obtained according to K and Filed
General instructions
slightly
Java operating Redis
Next, how to use Java to manipulate Redis
Maven build
For the Java operation Redis, the following Maven dependencies need to be introduced
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
Copy the code
Write the following Java test code to see if remote Redis can be accessed normally. If PONG is printed out, Redis can be accessed
@Test
public void get(a) {
Jedis jedis = new Jedis("81.69.231.87".6379);
jedis.auth("123456");
System.out.println(jedis.ping());
}
Copy the code
After that, try using Jedis to complete the Redis data operation, which is consistent with the Redis instruction
// Manipulate Strings
jedis.select(1);
jedis.mset("a"."1"."b"."2"."c"."3");
List<String> stringList = jedis.mget("a"."b"."c");
for (String str : stringList) {
System.out.println(str);
}
// Operate the hashes type
jedis.select(2);
jedis.hset("a"."name"."zhangsan");
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name"."lisi");
hashMap.put("age"."20");
jedis.hset("b", hashMap);
Map<String, String> a = jedis.hgetAll("a");
Map<String, String> b = jedis.hgetAll("b");
List<String> hmget = jedis.hmget("b"."name"."age");
System.out.println(Arrays.toString(hmget.toArray()));
System.out.println(jedis.hget("a"."name"));
Copy the code
summary
It is mainly the construction of Redis environment, as well as the simplest type and command operation. Redis did not find a suitable, legitimate remote operation tool. It is recommended to do it directly in Xshell, Xshell has education authorization (just email)