当前位置:网站首页>Introduction to redis
Introduction to redis
2022-07-25 11:58:00 【Hua Weiyun】
Redis Relevant concepts
Redis It's a high-performance NOSQL Series of non relational databases
What is? NOSQL
NoSQL(NoSQL = Not Only SQL), meaning “ not only SQL”, It's a new database concept , A general term for a non-relational database .
With the Internet web2.0 The rise of websites , Traditional relational databases are coping web2.0 Website , Especially large scale and high concurrency SNS Type of web2.0 Pure dynamic website has appeared to be powerless , A lot of insurmountable problems were exposed , The non-relational database has developed rapidly due to its own characteristics .NoSQL The emergence of database is to solve the challenge of large data sets and multiple data types , Especially big data application problem .
NOSQL Compared with relational databases
advantage :
- cost :nosql The database is simple and easy to deploy , It's basically open source software , It doesn't need to be like using oracle That would cost a lot of money to buy and use , It's cheaper than a relational database .
- Query speed :nosql The database stores the data in the cache , Relational databases store data on hard disks , Natural query speed is far less than nosql database .
- The format of data storage :nosql The storage format is key,value form 、 Document form 、 Picture form and so on , So you can store basic types and various formats such as objects or collections , The database only supports basic types .
- Extensibility : Relational databases are similar join The limitations of such a multi table query mechanism make it difficult to extend .nosql Better database scalability .
shortcoming :
- Limited tools and data for maintenance , because nosql It's a new technology , Not with relational databases 10 A few years of technology are on the same day .
- Don't offer right sql Support for , If not sql Such industrial standards , Will produce certain user's study and use cost .
- The transaction processing of relational database is not provided .
Advantages of non relational database
- performance NOSQL It's based on key value pairs , You can imagine the correspondence between the primary key and the value in the table , And there's no need to go through SQL Layer resolution , So the performance is very high .
- Extensibility is also based on key value pairs , There is no coupling between the data , So it's very easy to expand horizontally .
The advantages of relational databases
- Complex queries can be used SQL Statement is convenient to do very complex data query between one table and multiple tables .
- Transaction support enables data access requirements with high security performance to be realized . For these two types of databases , The advantage of the other side is its weakness , vice versa .
summary
Relational database and NoSQL Databases are not opposites, but complementary relationships , That is, relational databases are usually used , Suitable for use in NoSQL When you use NoSQL database ,
Give Way NoSQL Database makes up for the deficiency of relational database .
Generally, the data is stored in a relational database , stay nosql Back up the data stored in the relational database in the database
Mainstream NOSQL product
Key value (Key-Value) Storage database
Related products : Tokyo Cabinet/Tyrant、Redis、Voldemort、Berkeley DB
Typical applications : Content caching , Mainly used to handle high access loads with large amounts of data .
Data model : A series of key value pairs
advantage : A quick query
Inferiority : The stored data lacks structure
Column store database
Related products :Cassandra, HBase, Riak
Typical applications : Distributed file system
Data model : Store as a column cluster , Store the same column of data together
advantage : Fast search speed , High scalability , Easier to distribute
Inferiority : Relatively limited function
Document database
Related products :CouchDB、MongoDB
Typical applications :Web application ( And Key-Value similar ,Value It's structured )
Data model : A series of key value pairs
advantage : Data structure requirements are not strict
Inferiority : Poor query performance , And the lack of a unified query syntax
graphics (Graph) database
Correlation database :Neo4J、InfoGrid、Infinite Graph
Typical applications : Social networks
Data model : The graph structure
advantage : Using graph structure correlation algorithm .
Inferiority : You need to compute the whole graph to get the result , It is not easy to do distributed cluster solutions .
What is? Redis
Redis Yes, it is C An open source high performance key value pair for language development (key-value) database , Official test data ,50 Individual concurrent execution 100000 A request , The speed of reading is zero 110000 Time /s, The speed of writing is 81000 Time /s , And Redis Multiple key value data types are provided to meet the storage requirements in different scenarios , So far, Redis The supported key value data types are as follows :
- String type string
- Hash type hash
- List the type list
- Collection types set
- Ordered set type sortedset
redis Application scenarios of
- cache ( Data query 、 Short connection 、 News content 、 Product content and so on )
- A list of online friends in a chat room
- Task queue .( seckill 、 Rush purchase 、12306 wait )
- Application ranking
- Website visit statistics
- Data expiration processing ( It can be accurate to milliseconds )
- In distributed cluster architecture session Separate
Download and install
- Official website :https://redis.io
- Chinese net :http://www.redis.net.cn/
- window The detailed steps of version download and installation can be seen in my article :Windows10 Install the install free version redis
The command operation
redis Data structure of
redis What's stored is :key,value Formatted data , among key All strings ,value Yes 5 Different data structures .
value Data structure of :
- String type string
- Hash type hash : map Format
- List the type list : linkedlist Format . Duplicate elements are supported
- Collection types set : Duplicate elements are not allowed
- Ordered set type sortedset: Duplicate elements are not allowed , And the elements have order
String type string
- Storage :
set key value
127.0.0.1:6379> set username zhanjqOK- obtain :
get key
127.0.0.1:6379> get username"zhanjq"- Delete :
del key
127.0.0.1:6379> del username(integer) 1Hash type hash
- Storage : hset key field value
127.0.0.1:6379> hset myhash username zhanjq(integer) 1127.0.0.1:6379> hset myhash password 123(integer) 1 - obtain :
hget key field: Get the specified field Corresponding value
hgetall key: Get all field and value
127.0.0.1:6379> hgetall myhash1) "username"2) "zhanjq"3) "password"4) "123456"- Delete :
hdel key field
127.0.0.1:6379> hdel myhash username(integer) 1 List the type list: You can add an element to the head of the list ( On the left ) Or tail ( On the right )
- add to :
lpush key value: Add elements to the left table of the listrpush key value: Add elements to the right side of the list
127.0.0.1:6379> lpush myList b(integer) 1127.0.0.1:6379> lpush myList a(integer) 2127.0.0.1:6379> rpush myList c(integer) 3- obtain :
lrange key start end: Scope acquisition
127.0.0.1:6379> lrange myList 0 -11) "a"2) "b"3) "c"- Delete :
lpop key: Delete the leftmost element of the list , And return the element torpop key: Delete the rightmost element of the list , And return the element to
127.0.0.1:6379> lpop myList"a"Collection types set : Duplicate elements are not allowed
- Storage :
sadd key value
127.0.0.1:6379> sadd myset a b c(integer) 3127.0.0.1:6379> sadd myset a(integer) 0- obtain :
smembers key: obtain set All elements in the collection
127.0.0.1:6379> smembers myset1) "c"2) "a"3) "b"- Delete :
srem key value: Delete set Some element in the collection
127.0.0.1:6379> srem myset a(integer) 1127.0.0.1:6379> smembers myset1) "c"2) "b"Ordered set type sortedset: Duplicate elements are not allowed , And the elements have order . Each element is associated with a double Score of type .redis It's the scores that sort the members of a collection from small to large .
- Storage :
zadd key score value
127.0.0.1:6379> zadd mysort 60 zhangsan(integer) 1127.0.0.1:6379> zadd mysort 50 lisi(integer) 1127.0.0.1:6379> zadd mysort 80 wangwu(integer) 1- obtain :
zrange key start end [withscores]: Sort by score from small to large
notes :0 Represents the first element ,-1 Represents the last element ,-2 That's the penultimate element , And so on
127.0.0.1:6379> zrange mysort 0 -11) "lisi"2) "zhangsan"3) "wangwu"127.0.0.1:6379> zrange mysort 0 -1 withscores1) "lisi"2) "50"3) "zhangsan"4) "60"5) "wangwu"6) "80"zrevrange key start end: Sort by score from big to small , How many top ranking data can be obtained .
127.0.0.1:6379> zrevrange mysort 0 -11) "wangwu"2) "zhangsan"3) "lisi"zrank key k: Return value k In collection key Number one in the middle , Is in accordance with the v From small to large . Come back first 0, Second return 1, And so on .zrevrank key k : ditto , The difference is , according to k From big to small .
127.0.0.1:6379> zrank mysort lisi(integer) 0127.0.0.1:6379> zrevrank mysort lisi(integer) 2zcard: Returns the number of elements in the collection .
127.0.0.1:6379> zcard mysort(integer) 3zscore key k: Take out the assembly key The middle key is k Corresponding value v.
127.0.0.1:6379> zscore mysort zhangsan"60"- to update :
zincrby key num k: To assemble key The elements in k add num, Value for integer .
such as zincrby mysort 30 zhangsan, to id by zhangsan Plus 30 A number of . here zscore mysort zhangsan The result is 60+30 by 90.
127.0.0.1:6379> zincrby mysort 30 zhangsan"90"127.0.0.1:6379> zscore mysort zhangsan"90"- Delete :
zrem key k: Delete the specified element in the collection .
127.0.0.1:6379> zrem mysort zhangsan(integer) 1General Command
keys *: Query all the keystype key: Get the key corresponding to value The type of
127.0.0.1:6379> type usernamestring127.0.0.1:6379> type myhashhash127.0.0.1:6379> type myListlist127.0.0.1:6379> type mysetset127.0.0.1:6379> type mysortzsetdel key: Delete specified key value
Persistence
Redis It's a memory database , When redis Server restart , Get computer restart , Data will be lost , We can redis The data in memory is persisted and saved to the file on the hard disk .
Redis Persistence mechanism
RDB
- RDB: Default mode , There is no need to configure , This mechanism is used by default
At certain intervals , testing key The change of , Then persist the data - edit redis.windwos.conf file
RDB Default persistence policy :
# RDB Core rule configuration save < Specify the time interval > < Perform the specified number of update operations >, If the conditions are met, the data in memory will be synchronized to the hard disk # If you don't want to use RDB programme , You can put save "" The comment opens , The following three comments close # You can also define how many changes are written to disk in how many seconds # save ""#after 900 sec (15 min) if at least 1 key changed(15 At least one in a minute key Value change , Then write the data snapshot in memory to disk .)save 900 1#after 300 sec (5 min) if at least 10 keys changed(300 second (5 minute ) At least 10 individual key Value change , Then write the data snapshot in memory to disk .)save 300 10#after 60 sec if at least 10000 keys changed(300 second (5 minute ) At least 10000 individual key Value change , Then write the data snapshot in memory to disk .)save 60 10000# When RDB After a persistence error , Is the work still going on ,yes: Make sure to stop working ,no: You can go ahead with the work , Can pass info Medium rdb_last_bgsave_status understand RDB Is persistence wrong stop-writes-on-bgsave-error yes# Configure whether to compress data when storing to a local database , The default is yes.# Redis use LZF Compression way , But it took a little CPU Time for . If this option is turned off , But it can make the database files huge . Recommended Opening .rdbcompression yes# Check if rdb file ; from rdb The fifth version of the format begins , stay rdb The end of the document will be accompanied by CRC64 Checksum .# This is good for file fault tolerance , But in preservation rdb When you file , There will be a general idea 10% Loss of performance , So if you're after high performance , You can turn off the configuration .rdbchecksum yes# Specifies the local database file name , The default dump.rdbdbfilename dump.rdb- Restart redis The server , And specify the profile name
F:\developer\redis\Redis-x64-3.2.100>redis-server.exe redis.windows.conf
AOF
- AOF: How to log , The operation of each command can be recorded . After every command operation , Persistent data
- edit redis.windwos.conf file
# close aofappendonly no# Turn on aofappendonly yes# Persist every second appendfsync everysec # Every operation is persisted appendfsync always# No persistence appendfsync no Query persistence execution INFO peresistence
Carry out orders INFO peresistence View persistence execution
127.0.0.1:6379> INFO persistence# Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1651568620rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:0rdb_current_bgsave_time_sec:-1aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:okSome parameter descriptions :
- loading: Identifies whether the server is loading persistent files
- rdb_changes_since_last_save: Indicates the last successful generation rdb The number of commands written to the file .
- rdb_bgsave_in_progress: Indicates whether the server is creating rdb file .
- rdb_last_save_time: Indicates the last successful generation rdb The time stamp of the file .
- rdb_last_bgsave_status: Identify the most recent rdb Is persistence successful .
- rdb_last_bgsave_time_sec: Indicates the last successful generation rdb Time consumed by files .
- rdb_current_bgsave_time_sec: Indicates that the current server is creating rdb The operation of the file has consumed time
- rdb_last_cow_size: Express RDB In the process, the parent process and the child process compare how many modification operations have been performed .
Java client Jedis
Jedis: a java operation redis Database tools .
Open source address :https://github.com/redis/jedis
Use steps :
- pom The file import jedis Coordinates of
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.2</version></dependency>- Use
//1. Get the connection Jedis jedis = new Jedis("localhost",6379);//2. operation jedis.set("username","zhanjq");//3. Close the connection jedis.close();If set redis password , You need to add this line after getting the connection
//1. Get the connection Jedis jedis = new Jedis("localhost",6379);// If you have a password to set, you need to add this line , Set the connection password jedis.auth("123456");//2. operation jedis.set("username","zhanjq");//3. Close the connection jedis.close();Otherwise, the operation will report an error :redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required
Jedis Operate all kinds of redis Data structure in
String type string
set
get
//1. Get the connection Jedis jedis = new Jedis();// If you use null parameters to construct , The default value is "localhost",6379 port //2. operation // Storage jedis.set("username","zhanjq");// obtain String username = jedis.get("username");System.out.println(username);// have access to setex() Method to store a that can specify an expiration time key valuejedis.setex("activecode",20," Hey, hey, I'm still ");// take activecode:hehe Key value pairs are stored redis, also 20 Automatically delete the key value pair after seconds //3. Close the connection jedis.close();Hash type hash : map Format
hset
hget
hgetAll
//1. Get the connection Jedis jedis = new Jedis();// If you use null parameters to construct , The default value is "localhost",6379 port //2. operation // Storage hashjedis.hset("user","name","lisi");jedis.hset("user","age","23");jedis.hset("user","gender","female");// obtain hashString name = jedis.hget("user", "name");System.out.println(name);// obtain hash All of the map Data in Map<String, String> user = jedis.hgetAll("user");// keysetSet keySet = user.keySet();for (String key : keySet) {// obtain valueString value = user.get(key);System.out.println(key + ":" + value);}//3. Close the connection jedis.close();List the type list : linkedlist Format . Duplicate elements are supported
lpush / rpush
lpop / rpop
lrange start end : Scope acquisition
//1. Get the connection Jedis jedis = new Jedis();// If you use null parameters to construct , The default value is "localhost",6379 port //2. operation // list Storage jedis.lpush("mylist","a","b","c");// Save from left jedis.rpush("mylist","a","b","c");// Save... From the right // list Scope acquisition List mylist = jedis.lrange("mylist", 0, -1);System.out.println(mylist);// list eject String element1 = jedis.lpop("mylist");//cSystem.out.println(element1);String element2 = jedis.rpop("mylist");//cSystem.out.println(element2);// list Scope acquisition List mylist2 = jedis.lrange("mylist", 0, -1);System.out.println(mylist2);//3. Close the connection jedis.close();Collection types set : Duplicate elements are not allowed
sadd
smembers: Get all the elements
//1. Get the connection Jedis jedis = new Jedis();// If you use null parameters to construct , The default value is "localhost",6379 port //2. operation // set Storage jedis.sadd("myset","java","php","c++");// set obtain Set myset = jedis.smembers("myset");System.out.println(myset);//3. Close the connection jedis.close();Ordered set type sortedset: Duplicate elements are not allowed , And the elements have order
zadd
zrange
//1. Get the connection Jedis jedis = new Jedis();// If you use null parameters to construct , The default value is "localhost",6379 port //2. operation // sortedset Storage jedis.zadd("mysortedset",3," Arthur ");jedis.zadd("mysortedset",30," Hou Yi ");jedis.zadd("mysortedset",55," The Monkey King ");// sortedset obtain Set mysortedset = jedis.zrange("mysortedset", 0, -1);System.out.println(mysortedset);//3. Close the connection jedis.close();Jedis Connection pool : JedisPool
Use :
- establish JedisPool Connect pool objects
- Calling method getResource() Method to get Jedis Connect
//0. Create a configuration object JedisPoolConfig config = new JedisPoolConfig();config.setMaxTotal(50);config.setMaxIdle(10);//1. establish Jedis Connect pool objects JedisPool jedisPool = new JedisPool(config,"localhost",6379);//2. Get the connection Jedis jedis = jedisPool.getResource();//3. Use jedis.set("hehe","heihei");//4. close Return to connection pool jedis.close();jedis.properties :
host=127.0.0.1port=6379maxTotal=50maxIdle=10Connection pool tool class :
public class JedisPoolUtils { private static JedisPool jedisPool; static{ // Read configuration file InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties"); // establish Properties object Properties pro = new Properties(); // Associated files try { pro.load(is); } catch (IOException e) { e.printStackTrace(); } // get data , Set to JedisPoolConfig in JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal"))); config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle"))); // initialization JedisPool jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port"))); } /** Get the connection method / public static Jedis getJedis(){ return jedisPool.getResource(); }}边栏推荐
- "Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities
- Talking about Devops monitoring, how does the team choose monitoring tools?
- Review in the middle of 2022 | understand the latest progress of pre training model
- Transformer变体(Sparse Transformer,Longformer,Switch Transformer)
- Web APIs(获取元素 事件基础 操作元素)
- 什么是全局事件总线?
- W5500多节点连接
- Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
- dirReader.readEntries 兼容性问题 。异常错误DOMException
- The first C language program (starting from Hello World)
猜你喜欢
随机推荐
What is the difference between session and cookie?? Xiaobai came to tell you
硬件连接服务器 tcp通讯协议 gateway
已解决 Files‘ name is invalid or does not exist (1205)
MIIdock简述
程序员送给女孩子的精美礼物,H5立方体,唯美,精致,高清
W5500通过上位机控制实现调节LED灯带的亮度
toString()与new String()用法区别
Differences in usage between tostring() and new string()
PHP 上传ftp路径文件到外网服务器上 curl base64图片
W5500 upload temperature and humidity to onenet platform
JaveScript循环
【USB设备设计】--复合设备,双HID高速(64Byte 和 1024Byte)
winddows 计划任务执行bat 执行PHP文件 失败的解决办法
什么是全局事件总线?
JS常用内置对象 数据类型的分类 传参 堆栈
JS process control
pycharm连接远程服务器ssh -u 报错:No such file or directory
What is the global event bus?
矩阵的特征值和特征向量
[USB device design] - composite device, dual hid high-speed (64BYTE and 1024byte)






![[MySQL learning 08]](/img/9e/6e5f0c4c956ca8dc31d82560262013.png)

