Getting started with Redis
1.NOSQL concept
redis is a high-performance NOSQL series non-relational database
-
What is NOSQL
NoSQL (NoSQL = Not Only SQL), which means "not just SQL", is a new database concept and generally refers to non-relational databases.
The word NoSQL also has a down-to-earth explanation as "no SQL". Because it is a non-relational database, it does not support unified SQL query standards (structured query language) and does not use SQL for data processing. , but has its own independent processing API
-
The background of the development of NOSQL
With the gradual rise of Internet web2.0 websites, traditional relational databases have become unable to cope with web2.0 websites, exposing many problems. However, non-relational databases are very fast due to their own speed. Features have flourished.
The emergence of NoSQL databases is to solve the challenges brought by large-scale data collections and multiple data types, especially the problems in the application direction of big data.
-
Comparing NOSQL and relational databases
-
advantage:
- Cost: NoSQL database is simple and easy to deploy. It is basically open source software. It does not require a large cost to purchase and use like Oracle. In comparison, relational database is cheaper.
- Query speed: NoSQL database stores data in the cache, while relational databases store data on the hard disk. Naturally, the query speed is far slower than that of NoSQL database.
- Storage data format: The storage format of NoSQL is key and value form, document form, picture form, etc., so it can store basic types, objects or collections and other formats, while the database only supports basic types.
- Scalability: Relational databases have limitations in multi-table query mechanisms such as join, which makes expansion difficult.
-
shortcoming:
- The tools and materials for maintenance are limited because NoSQL is a newer technology and cannot be compared with the relational database, which is a technology that has been around for more than ten years.
- It does not provide support for sql. If query structure language such as sql is not supported, certain learning and usage costs will be incurred.
- Does not provide transaction processing similar to a relational database.
-
-
Advantages of non-relational databases:
- Performance NOSQL is based on key-value pairs, which can be imagined as a simple map structure, and does not need to be parsed by SQL language, so the performance is very high .
- Scalability is also because it is based on key-value pairs and there is no coupling between data, so it is easy to expand horizontally .
-
Advantages of relational database:
- Complex queries can use SQL statements to easily perform very complex data queries between one table and multiple tables.
- Transaction support enables data access requirements with high security performance to be realized.
-
Summarize
- Relational databases and NoSQL databases are not antagonistic but complementary, that is, relational databases are usually used, and NoSQL databases are used when NoSQL is suitable, so that NoSQL databases can make up for the shortcomings of relational databases.
- Generally, we store data in a relational database, and back up and store part of the data in the relational database in the nosql database.
-
The mainstream NOSQL products are as follows:
- Key-Value storage database
Related products: Tokyo Cabinet/Tyrant,Redis, Voldemort, Berkeley DB
typical applications: Content caching, mainly used to handle high access loads of large amounts of data.
Data model: Key-value pairs
Advantages: Fast query
Disadvantages: The stored data lacks structure - Column storage database
Related products: Cassandra, HBase, Riak
Typical application: distributed file system
Data model: Column cluster storage, storing the same column data together
Advantages: fast search speed, strong scalability, easier to distribute Extension
Disadvantages: Functions are relatively limited - Document database
Related products: CouchDB, MongoDB
Typical applications: Web applications (similar to Key-Value, Value is structured)
Data model: A series of key -value pairs
Advantages: Data structure requirements are not strict
Disadvantages: Query performance is not high, and Lack of unified query syntax - Graph database
Related databases: Neo4J, InfoGrid, Infinite Graph
Typical applications: Social networks
Data model: Graph structure
Advantages: Utilize graph structure-related algorithms.
Disadvantages: The entire graph needs to be calculated to get the result, and it is not easy to implement a distributed cluster solution.
- Key-Value storage database
2.What is Redis
Redis is an open source, high-performance key-value database developed in C language. According to official test data, 50 concurrent executions of 100,000 requests resulted in a read speed of 110,000 times/s, while the write speed was 110,000 times/s. The speed is 81,000 times/s, and Redis provides a variety of key-value data types to adapt to storage needs in different scenarios. The key-value data types supported by Redis so far are as follows :
- String type string
- Hash type hash
- List type list
- Collection type set
- Sorted set type sortedset
Application scenarios of redis
- Caching (data query, short connection, news content, product content, etc.)
- Chat room's online friends list
- Task queue. (Flash sale, rush sale, 12306, etc.)
- Application rankings
- Website visit statistics
- Data expiration processing (accurate to milliseconds)
- Session separation in distributed cluster architecture
Add two links:
Redis official websiteRedis
Chinese website
3.Download and install Redis under Windows
First of all, in the installation tutorial of redis Chinese website, three installation methods are provided, namely
- Installation under Windows
- Installation under Linux
- Installation under Ubuntu
However, the versions here are relatively low, and it is not recommended to download.
The highest version here is only 2.4x.
Here is a recommended address, the version is relatively new:
https://github.com/microsoftarchive/redis/releases
The page is as follows:
Find what you want For the downloaded version, click in to download directly. The file is very small, because we are using it under Windows, so we downloaded the zip directly. The green version can be directly decompressed and used. After decompression, the directory file is as follows: You can delete all useless
things
. , the directory after sorting is:
Among them, we will mainly use the following three files:
redis.windows.conf: configuration file
redis-cli.exe: redis client
redis-server.exe: redis server
4.Redis data types and simple operations
Double-click redis-server.exe (redis server) , and the following interface will appear: The port number is 6379.
Do not close this interface. Closing this interface is equivalent to closing the redis server.
Then double-click to open **redis-cli.exe (client)**, and you can start operating redis on the client console.
The five data structure types of redis were mentioned above:
- Redis stores data in key and value formats, where keys are all strings and value has 5 different data structures.
- value data structure:
- String type string
- Hash type hash: map format
- List type list: linkedlist format. Supports repeated elements
- Collection type set: Duplicate elements are not allowed
- Sorted set type sortedset: no duplicate elements are allowed, and the elements are in order
- value data structure:
They each have corresponding independent commands to operate. There are related tutorials on our redis Chinese website: The
examples are as follows:
The Chinese website will have a more complete command summary and click-in example commands, because the corresponding command content here is relatively small. Many, here we will give a simple usage example and summary of some simple content that must be mastered. You can learn and master the other specific commands yourself when you have time:
- String type string
① Storage: set key value
② Obtain: get key
③ Delete: del key
When you type redis command keywords such as set, he will have a follow-up prompt.
Simple operation:
- Hash type hash: map format
① Storage: hset key field value
② Obtain: hget key field
③ Delete: hset key field value
Here, the deletion of the entire hash value requires the use of a general command. This will be said at the end.
Simple operation:
- List type list: linkedlist format. Supports repeated elements.
You can add an element to the head (left) or tail (right) of the list
① Add:
lpush key value: Add the element to the left table of the list
rpush key value: Add the element to the right side of the list
② Get: lrange key start end
③ Delete:
lpop key: Delete the leftmost element of the list and return the element
rpop key: Delete the rightmost element of the list and return the element
Simple operation:
4. Set type set: Duplicate elements are not allowed
① Add: sadd key value
② Get: smembers key Get all elements in the set collection
③ Delete: srem key value Delete an element in the set collection
A simple operation:
5. Sorted set type sortedset: Duplicate elements are not allowed, and the elements are in order.
Each element is associated with a double type score. Redis uses scores to sort the members of the collection from small to large.
① Add: zadd key score value
② Get: zrange key start end Add the [withscores] parameter to print out the corresponding scores together
③ Delete: zrem key value
A simple operation:
common commands:
- keys *: Query all keys
- type key: Get the type of value corresponding to the key
- del key: delete the specified key value
Just do this:
5.Redis persistence
Redis is an in-memory database. When the redis server is restarted or the computer is restarted, the data will be lost. We can persist the data in the redis memory to a file on the hard disk.
Redis has provided us with two persistence mechanisms:
-
RDB: Default mode, no configuration is required. This mechanism is used by default,
also called snapshot mode. It will be persisted after a certain period of time. This persistence trigger condition is set by ourselves in the configuration file. Since The granularity of this persistence method may be relatively large, and data loss may occur.* 在一定的间隔时间中,检测key的变化情况,然后持久化数据 1. 编辑redis.windwos.conf文件 # after 900 sec (15 min) if at least 1 key changed save 900 1 # after 300 sec (5 min) if at least 10 keys changed save 300 10 # after 60 sec if at least 10000 keys changed save 60 10000 2. 重新启动redis服务器,并指定配置文件名称 D:\...\redis\windows-64\redis>redis-server.exe redis.windows.conf
We open and edit the configuration file:
find SNAPSHOTTING, which means snapshot.
We add the sentence save 10 1, which means
persistence will be triggered when at least one key value is modified in ten seconds. Note that the query here will not trigger persistence. oriented, let’s start the service. Enter
the folder where your redis-server is located, shift + right mouse button, click to open the command window here , we use the startup method with the configuration file: redis-server.exe redis.windows. conf
The author reported an error when starting here: Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
According to the query, it may be the reason why the client did not exit successfully. The solution is as follows:
Solving Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
later started successfully:
After a wave of operations, a .rdb file will appear.
After you restart the service, the previously stored key values will always exist in your memory data until you delete them. What needs to be noted here is the frequency of snapshots, that is its triggering conditions.
The second persistence method:
- AOF: Logging method, which can record the operation of each command. Data can be persisted after each command operation
- Edit the redis.windwos.conf file
appendonly no (turn off aof) --> appendonly yes (turn on aof)
# appendfsync always: Persistence is performed for every operation
appendfsync everysec: Persistence is performed every second
# appendfsync no: Not performed Persistence
Here we find the additional mode APPEND ONLY MODE,
select to turn on aof
appendonly yes (turn on aof),
scroll down slightly, and find the persistence granularity setting corresponding to aof. We can find that the persistence granularity of aof is very small, very fine, but also very The consumption of resources depends on the business data.
Here we still start redis with configuration file parameters, and make any key value settings, add and modify it.
After starting, the .aof file is directly generated, which is the file that stores the command records. The size is small because we have no operations. is 0, we can do a few operations: we
can find that its size has changed. We close the server and open it again to see if the key value is still there
.
6.Redis Java client Jedis operation
Jedis: A Java tool for operating redis database. You can use it after downloading the jar package of jedis.
Simple example:
public class JedisTest {
@Test
public void test() {
// 获取连接
Jedis jedis = new Jedis("localhost", 6379);
// 操作
/**
* String类型操作
* 添加,修改:set
* 获取(单个):get
* 删除:del
*/
jedis.set("name", "zhangsan");
String name = jedis.get("name");
jedis.del("name");
// 设置带失效时间的key-value
jedis.setex("code", 20, "cfl777");
/**
* hash类型操作
* 添加,删除:hset
* 获取(单个):hget
* 删除:hdel
* 获取(全部):hgetall
*/
jedis.hset("person", "name", "cfl");
jedis.hset("person", "age", "23");
String hget = jedis.hget("person", "name");
Map<String, String> person = jedis.hgetAll("person");
jedis.hdel("person", "name");
/**
* list类型操作
* 添加:lpush,rpush
* 获取(范围):lrange start end
* 删除:rpop,lpop
*/
jedis.lpush("list", "aaa");
jedis.lpush("list", "bbb");
jedis.rpush("list", "ccc");
List<String> list = jedis.lrange("list", 0, -1);
String lpop = jedis.lpop("list");
String rpop = jedis.rpop("list");
/**
* set类型操作 不允许重复元素
* 添加:sadd
* 获取(全部):smembers
* 删除:srem
*/
jedis.sadd("myset", "aaa", "bbb");
Set<String> myset = jedis.smembers("myset");
jedis.srem("myset", "aaa");
/**
* sortedSet 带排序(得分)的set → ztree → z
* 添加:zadd
* 获取(范围):zrange
* 删除:zrem
* 可选项:withscores,表示元素值得同时表示出其得分(排序)
*/
jedis.zadd("zset", 3, "aaa");
jedis.zadd("zset", 1, "bbb");
jedis.zadd("zset", 2, "ccc");
Set<String> zset = jedis.zrange("zset", 0, -1);
Set<Tuple> zset1 = jedis.zrangeWithScores("zset", 0, -1);
jedis.zrem("zset", "bbb");
/**
* 通用方法
*/
// 通过正则获取匹配到的key
Set<String> keys = jedis.keys("*");
// 直接删除对应key的元素
jedis.del("key");
// 获取此key对应的元素值类型
String type = jedis.type("key");
// 关闭连接
jedis.close();
}
}
And the connection pool provided by Jedis itself:
public class JedisPoolTest {
public static void main(String[] args) {
// 创建连接池配置文件
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(50);
// ...
// 创建连接池
JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379);
// 获取连接
Jedis jedis = jedisPool.getResource();
// 使用...
// 关闭连接,归还至连接池
jedis.close();
}
}
7.Redis advanced client Lettuce
I would like to mention Lettuce here. Although, like Jedis, they are Java encapsulation of redis operation commands, Lettuce has been recognized by the Spring ecosystem and has been integrated into the spring-data-redis driver. It must have its own advantages. The author only has a brief understanding here, so here is a link that I have read before and thought it was very good, so that we can learn and encourage each other: Detailed explanation of
Redis advanced client Lettuce