当前位置:网站首页>Redis6 note02 configuration file, publish and subscribe, new data type, jedis operation
Redis6 note02 configuration file, publish and subscribe, new data type, jedis operation
2022-06-25 11:18:00 【GYQ1】
Redis Configuration file details
Units Company
Configure size units , Some basic units of measurement are defined at the beginning , Only support bytes, I won't support it bit
Case insensitive

INCLUDES contain

Network related configuration
NETWORK Next , The default is as follows , Indicates that only local access requests can be accepted
![]()
Without writing , Unlimited acceptance of any ip Address access , The address of your application server must be written in the production environment ; The server needs remote access , So you need to comment it out
![]()
If open protected-mode, Then there's no setting bind ip And without a password ,Redis Only local responses are allowed
![]()
Change it to no
![]()
Port number
![]()
tcp-backlog
Set up tcp Of backlog,backlog It's actually a connection queue ,backlog The sum of the queues = Three handshake queues not completed + Three handshake queues completed
In a high concurrency environment you need a high backlog Value to avoid slow client connection problems
Be careful Linux The kernel will reduce this value to /proc/sys/net/core/somaxconn Value (128), So we need to confirm the increase /proc/sys/net/core/somaxconn and /proc/sys/net/ipv4/tcp_max_syn_backlog(128) Two values to achieve the desired effect

timeout
![]()
Timeout period of no operation after connection , You need to reconnect after timeout ,0 It means never time out
tcp-keepalive

Check whether the current connection is still in operation ( Alive ) Time interval of
General Universal
daemonize
Is it a background process , Set to yes, Daemon , Background start

pidfile
Deposit pid The location of the file , Each instance produces a different pid file

loglevle
The level of logging

logfile
Log file output path

databases
Set the default number of libraries 16, Default database 0, have access to select<did> The command specifies the database on the connection id

SECURITY Security
Set the password , The default is not set

Access password view , Set up

Set the password in the command , It's just temporary . restart redis The server , The password is restored
Permanent settings , You need to set... In the configuration file
LIMITS Limit
maxclients
Set up redis How many clients can I connect to at the same time
By default 10000 A client
If this limit is reached ,redis New connection requests will be rejected , And send... To these connection requesters "max number of clients reached" In response to

maxmemory
It is suggested that , otherwise , Fill up the memory , Causing server downtime
Set up redis The amount of memory that can be used , Once the memory usage limit is reached ,redis Will attempt to remove internal data , Remove the rule by maxmemory-policy To specify the .
If redis Can't remove data in memory according to removal rules , Or set up “ It is not allowed to remove ”, that redis An error message will be returned for the instructions that need to apply for memory , such as SET,LPUSH etc. .
But for instructions without memory request , It will still respond normally , such as GET etc. . If your redis It is the Lord. redis( Description from redis), When setting the upper memory limit , You need to leave some memory space in the system for the synchronous queue cache , Only if you set up “ Do not remove ” Under the circumstances , I don't have to think about it .

maxmemory-policy


maxmemory-samples
Set the number of samples ,LRU Algorithm and minimum TTL Algorithms are not exact algorithms , It's an estimate , So you can set the sample size ,redis By default, so many key Not selected LRU the
General Settings 3-7 The number of , The smaller the value, the less accurate the sample is , But the performance consumption is small .

Publish and subscribe
What is publish and subscribe
Redis Publish subscribe (pub/sub) It's a message communication mode : sender (pub) Send a message , subscriber (sub) receive messages .
Redis Clients can subscribe to any number of channels .

Publish and subscribe command line implementation
Open a client subscription channel1

Open another client to channel1 Release the news hello
![]()
return 1 Is the number of subscribers
Open the first client to see the sent message

Redis6 New data types
Bitmaps
Modern computers use binary ( position ) As the basic unit of information ,1 Characters equal to 8 position , for example “abc” Strings are created by 3 Byte composition , But it is actually represented in binary when stored in the computer ,“abc“ Respectively corresponding ASCII The codes are 97,98,99, The corresponding binaries are 01100001,01100010 and 01100011 Here's the picture

Reasonable use of operation bits can effectively improve memory utilization and development efficiency .
Redis Provides Bitmaps This ” data type ” It can realize the alignment operation
Bitmaps Itself is not a data type , It's actually a string (key-value), But it can operate on the bits of a string .
Bitmaps A separate set of commands is provided , So in Redis Use in Bitmaps It's not quite the same as using strings . You can put Bitmaps Think of it as an array of bits , Each unit of an array can only store 0 and 1, The subscript of the array is in Bitmaps It's called offset .

Common commands
setbit key offset value: Set up Bitmaps The value of an offset in (0 or 1), Offset from 0 Start
example : Whether each individual user has visited the website and stored in Bitmaps in , Record the user you visited as 1, Do not visit users remember to do 0, Use the offset as the user id.
Set the number of the key offset The value of a bit ( from 0 Count up ), Suppose there are now 20 Users ,userid=1,6,11,15,19 's users visited the site , Then the current Bitmaps The initialization result is shown in Figure


Users of many applications id With a specified number ( Such as :10000) start , Direct users id and Bitmaps A certain amount of offset is bound to cause waste , The usual practice is to do it every time setbit The user will be id Subtract the specified number .
At first initialization Bitmaps when , If the offset is very large , The whole initialization process will be slow , It may cause Redis Blocking .
getbit key offset: obtain Bitmaps The value of an offset in , The value corresponding to the nonexistent offset is 0
![]()
bitcount key start end Statistics string from start byte To end The byte bit value is 1 The number of
![]()
bitop and(or/not/xor) destkey key
bitop Is a consistent operation , It can do more than one Bitmaps Of and intersection ,or Combine ,not Not ,xor XOR operation and save the result in destkey in

set and Bitmaps Comparison of

Obviously , Use in this case Bitmaps Can save a lot of memory space , In particular, the memory saved over time is still very considerable .

however Bitmaps It's not a panacea , Few independent visitors join the site every day , Then the comparison between the two is shown in the table below , Obviously , Use at this time Bitmaps Not really , Because most of the bits are 0.

HyperLogLog
At work , We often encounter statistical related functional requirements , For example, statistics of website page visits , have access to Redis Of incr,incrby Make it easy .
But like UV(UniqueVistor, Independent visitor ), Independent IP Count , How to solve the problem of searching the number of records and other problems that need to be overlapped ? The problem of finding the number of non repeating elements in a set is called the cardinality problem .
There are many solutions to the cardinality problem :
1、 The data is stored in Mysql In the table , Use distinct count Calculate the number of non duplicates
2、 Use Redis Provided hash,set,bitmaps And other data structures
The results of the above scheme are accurate , But as the data grows , Resulting in more and more occupied space , It is impractical for very large data sets
Whether it can reduce a certain accuracy to balance the storage space ?Redis Launched HyperLogLog
Redis HyperLogLog It's an algorithm for cardinality statistics ,HyperLogLog The advantages of , When the number or volume of input elements is very, very large , The space needed to calculate the cardinality is always fixed , And it's very small
stay Redis Inside , Every HyperLogLog Keys only cost 12KB Memory , So we can calculate the proximity 2^64 Cardinality of different elements . This is the same as calculating the cardinality , The more elements you consume, the more memory you consume in a collection .
however , because HyperLogLog Only the input elements will be used to calculate the cardinality , Instead of storing the input element itself , therefore HyperLogLog It can't be like a collection , Return the various elements of the input .
What is the cardinality ?
Like data sets {1,3,5,7,5,7,8}, So the cardinality set of this dataset is {1,3,5,7,8}, base ( Number of non repeating elements ) by 5. Cardinality estimation is within the range of acceptable error , Fast base calculation .
command
pfadd key element...: Add specified elements to HyperLogLog in

After joining , When the cardinality changes, it returns 1, Otherwise return to 0
pfcount key....: Calculation HLL The approximate cardinality of , Sure Calculate multiple HLL, use HLL Store daily UV, Calculate the usage of the week 7 Days of consolidation
![]()
pfmerge destkey sourcekey1 sourcekey2.....: Put one or more HLL The merged results are stored in another HLL

Geospatial
Redis3.2 Added right to GED Type of support .GEO,Geographic, Abbreviation for geographic information . This type of , Is the two-dimensional coordinates of the element , On the map is latitude and longitude .redis Based on this type , Longitude and latitude settings are provided , Inquire about , Range queries , Distance inquiry , Longitude and latitude Hash And so on .
command
geoadd key longgitude latitude member...: Add location ( precision , dimension , name )
![]()

geopos key member: Get the coordinate value of the specified area

geodist key member1 member2 [m|km|ft|mi]: Get the linear distance between two positions , Default meter

mi: miles ,ft: feet
georadius key longitude latitude radius [m|km|ft|mi], With the given latitude and longitude as the center , Find the elements in a certain radius

Jedis operation Redis6
Connect Redis
establish maven engineering
add to Jedis The required depend on
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>Java Connect Redis
It needs to be annotated first redis.conf in bind Below
![]()
close protected mode
![]()
see Linux System firewall status , Press q sign out
systemctl status firewalld
Turn off the firewall temporarily
systemctl stop firewalldtest

The return value is PONG The connection is successful
Test related operations
operation String type

operation list

operation set

operation hash

operation zset

One to one correspondence with command line commands .
Case study : Verification Code ( Six digit random verification code , Two minutes expired , Three times a day for each mobile number )
public class JedisDemo {
public static void main(String[] args) {
// User tips
System.out.println(" Please enter your mobile phone number ");
Scanner sc = new Scanner(System.in);
String tel = sc.next();
// Verify the number of times sent , Put the verification code in Redis
verifyCode(tel);
// The user enters the verification code
System.out.println(" Please enter the verification code received ");
String code = sc.next();
// Determine whether the verification code is correct
getRedisCode(tel, code);
}
// Generate verification code
public static String generateCode() {
Random random = new Random();
String code = "";
for (int i = 0; i < 6; i++) {
int rand = random.nextInt(10);
code += rand;
}
return code;
}
// Each mobile phone can only send verification code three times a day , Put the verification code in Redis in
public static void verifyCode(String tel) {
// establish Jedis object ,Redis Of the running server ip Address , Port number
Jedis jedis = new Jedis("192.168.199.129", 6379);
// Splicing key
// The number of times a cell phone sends key
String countKey = "VerifyCode" + tel + ":count";
// Verification Code key
String codeKey = "VerifyCode" + tel + ":code";
// Each mobile phone can only send three times at a time
String count = jedis.get(countKey);
if (count == null) {
// First send
jedis.setex(countKey, 24 * 60 * 60, "1");
} else if (Integer.parseInt(count) <= 2) {
// Send times plus 1
jedis.incr(countKey);
} else if (Integer.parseInt(count) > 2) {
// Has been sent three times
System.out.println(" Sent more than three times ");
jedis.close();
return;
}
// Send verification code to redis In the middle
String code = generateCode();
System.out.println(code);
jedis.setex(codeKey, 120, code);
jedis.close();
}
// Verification code verification
public static void getRedisCode(String tel, String code) {
// Connect Redis
Jedis jedis = new Jedis("192.168.199.129", 6379);
// Verification Code key
String codeKey = "VerifyCode" + tel + ":code";
String redisCode = jedis.get(codeKey);
// Judge
if (code.equals(redisCode)) {
System.out.println(" Verify success ");
} else {
System.out.println(" Validation failed ");
}
}
}边栏推荐
- 2022 PMP project management examination agile knowledge points (2)
- scrapy+scrapyd+gerapy 爬虫调度框架
- Advanced single chip microcomputer -- development of PCB (2)
- Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
- A program reflecting the characteristics of C language program structure
- Apache ShenYu 入門
- Multiple environment variables
- FPGA基于VGA显示字符及图片
- Continuous delivery jenkinsfile syntax
- Comparison between relu and SIGMOD
猜你喜欢

FPGA displays characters and pictures based on VGA

寿命分布 4种
![[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology](/img/82/8cac87231e51698ab17f1274b3a0bd.jpg)
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology

CMU提出NLP新范式—重构预训练,高考英语交出134高分

Open source invites you to participate in the openssf Open Source Security Online Seminar

C disk uses 100% cleaning method

At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world

Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework

记一次有趣的逻辑SRC挖掘

How to start the phpstudy server
随机推荐
[file containing vulnerability-03] six ways to exploit file containing vulnerabilities
【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
手机办理长投学堂证券开户靠谱安全吗?
Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
Ouverture de l'inscription | le troisième marathon des hackers de pagaie est arrivé comme prévu.
视频会议一体机的技术实践和发展趋势
Spannable and editable, spannablestring and spannablestring
动态规划解决股票问题(上)
GaussDB 集群维护案例集-sql执行慢
开源社邀请您参加OpenSSF开源安全线上研讨会
A program reflecting the characteristics of C language program structure
Compilation of learning from Wang Shuang (1)
贝叶斯
每日3题(3)-检查整数及其两倍数是否存在
SQL注入漏洞(繞過篇)
Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
Upload and modify the use of avatars
Five types of questions about network planning
Comparator(用于Arrays.sort)
GC