Disclosure of Invention
In order to overcome the problems, the invention aims to provide a method for detecting and slicing the value of the redis data cache, which splits the value of the giant redis data cache into a plurality of small values in a slicing way, thereby effectively solving the problems of network blocking and the like caused by overtime blocking of a client.
The invention adopts the following scheme that the method for detecting and slicing the value of the redis data cache comprises the following steps:
step S1, setting a threshold value of slow query of a redis data cache, acquiring redis reading time through time difference of log records before and after the redis data cache is read, and asynchronously writing keys of the slowly queried redis data cache into a log when the redis reading time exceeds the threshold value;
Step S2, monitoring a slow query log, reading a key of a redis data cache of the slow query, reading a value corresponding to the key of the redis data cache, if the value is smaller than 10KB, not performing operation, ending the flow, if the value is larger than 100KB, deleting the key of the redis data cache, and if the value is between 10KB and 100KB, entering step S3;
Step S3, performing group-wise slicing on the value of the redis data cache according to a preset value, wherein the content of each group of value is value_1, value_2 and value_3, and the like until the last slice is not added with a suffix to end;
And S4, providing a redis reading interface, reading rediss the value of the data cache key, if the value is the value of which the end of the value is used for indicating that the redis data cache is segmented, continuing to read the data of the key value_1 of the next data segment, and so on, reading until the last segment, and splicing the characters of all the segments and returning to the client.
Further, the implementation manner of the step S1 is further specifically that a threshold value of the redis slow query is set to be 100ms, the query exceeding 100ms is the slow query caused by too large value, the time difference between the log records before and after the redis is read is used for obtaining the reading time of the redis, if the threshold value exceeds 100ms, the redis data cache key of the slow query is asynchronously written into the log, and the mode of synchronously writing into the log is not adopted, so that the throughput of the system is not reduced.
Further, the implementation manner of the step S3 is further specifically that the value of the redis data cache is segmented according to a preset value, so that the situation that a single value is too large to cause a single timeout of a query is avoided, the grouping is performed in the mode of key: value_1, key_1: value_2, key_2: value_3, wherein N is an integer larger than 1, and the grouped segmentation set is { (key value, value_1), (key value_1, value_2) } (key value_N-1, value_N, value) and the like.
Further, the implementation manner of the step S4 is further specifically that a redis read interface is provided, a value of rediss data cache keys is read through the redis read interface, if the value is a value of which the end indicates that the redis data cache is fragmented, the data of the next data fragment key value_1 is continuously read, and the value corresponding to the data fragment key value_1 is ended with the end of the value_2, the data of the next data fragment key value_2 is continuously read, namely, the tail suffix of the value is used as an index to search for the same tail suffix of the key corresponding to the next data fragment, and if the last value of the data fragment has no suffix, the last value of the data fragment is regarded as being read to all the fragmented data, and then the value_1+value_2+ value_n+value is spliced to obtain complete data and is returned to the client.
The invention also provides a value detection and slicing system of the redis data cache, which comprises a redis reading time acquisition module, a value size detection module, a slicing module and a splicing and combining module;
The redis reading time acquisition module is used for setting a threshold value of the slow query of the redis data cache, acquiring the redis reading time through the time difference of log records before and after the redis data cache is read, and asynchronously writing keys of the slowly queried redis data cache into a log when the redis reading time exceeds the threshold value;
The value detection module monitors a slow query log, reads the key of the redis data cache of the slow query, reads the value corresponding to the key of the redis data cache, does not operate if the value is smaller than 10KB, ends the flow, deletes the key of the redis data cache if the value is larger than 100KB, and enters the fragmentation module if the value is between 10 and 100 KB;
The slicing module performs slicing according to groups of values of the redis data cache according to a preset value, wherein the content of each group of values is value_1, value_2 and value_3, and the like until the last slicing is not added with a suffix; the grouped set of tiles is { (key value, value_1), (key value_1, value_2) } (key value_n-1, value_n), (key value_n, value) };
And the splicing combination module provides a redis reading interface, reads the value of the rediss data cache key, continuously reads the data of the key value_1 of the next data fragment if the value is the value of which the end of the value is 1 and indicates that the redis data cache is fragmented, and similarly, pushes the last fragment to splice all the fragmented characters and returns the characters to the client.
Further, the implementation mode of the redis reading time acquisition module is further specifically that a threshold value of a redis slow query is set to be 100ms, the query exceeding 100ms is the slow query caused by too large value, the redis reading time is obtained through the time difference of log records before and after redis reading, if the threshold value exceeds 100ms, the redis data cache key of the slow query is asynchronously written into a log, and the mode of synchronously writing into the log is not adopted, so that the throughput of a system is not reduced.
Further, the implementation mode of the slicing module is further specifically that the value of the redis data cache is sliced according to a preset value, so that the situation that a single time-out is caused by query due to the fact that a single value is too large is avoided, the grouping mode is that the value is key 1, key 2, key 3, key N-1, key N, value is an integer larger than 1, and the sliced set after grouping is { (key value, value 1), (key value 1, value 2) } (key value N-1, value N, value) is avoided.
Further, the implementation mode of the splicing combination module is further specifically that a redis reading interface is provided, a value of rediss data cache keys is read through the redis reading interface, if the value is a value which ends with a value of_1 and indicates that the redis data cache is fragmented, data of a next data fragmentation key value_1 is continuously read, and the value corresponding to the data fragmentation key value_1 ends with a value of_2, the data of the next data fragmentation key value_2 is continuously read, namely, a tail suffix of the value is used as an index to search for the same tail suffix of the key corresponding to the next data fragmentation, and the last value of the data fragmentation is regarded as being read to all fragmented data if no suffix exists, and then the splicing value_1+value_2+value_N+value is obtained to obtain complete data and is returned to a client.
The invention has the beneficial effects that the invention can record the redis data cache slow query log asynchronously, and split the value of one huge redis data cache into a plurality of small values by detecting whether the key of the redis data cache of slow query is a redis large value or not and by a slicing way, thereby avoiding the overtime of a client caused by the too slow query of a single large value, changing the overtime reading of a single into the reading of a plurality of small values, and effectively slowing down the problems of network blocking caused by the overtime blocking of the client, and the like.
Detailed Description
The invention is further described below with reference to the accompanying drawings.
Referring to fig. 1, the method for detecting and slicing value of redis data cache according to the present invention includes the following steps:
Step S1, setting a threshold value of slow query of a redis data cache, acquiring redis reading time through a time difference recorded by the logs before and after the redis data cache is read, and asynchronously writing keys of the redis data cache of the slow query into the log when the redis reading time exceeds the threshold value, wherein the implementation mode of the step S1 is further specifically that the threshold value of the redis slow query is set to be 100ms, the query exceeding 100ms is the slow query caused by too large value, the reading time of the redis is obtained through the time difference recorded by the logs before and after the redis data cache, and if the time difference exceeds the threshold value of 100ms, the redis data cache keys of the slow query are asynchronously written into the log without adopting a mode of synchronously writing the log so as not to reduce the throughput of the system.
Step S2, monitoring a slow query log, reading a key of a redis data cache of the slow query, reading a value corresponding to the key of the redis data cache, if the value is smaller than 10KB, not performing operation, ending the flow, if the value is larger than 100KB, deleting the key of the redis data cache, and if the value is between 10KB and 100KB, entering step S3;
Step S3, performing group-wise slicing on the value of the redis data cache according to a preset value, wherein the content of each group of value is value_1, value_2 and value_3, and the like until the last slice is not added with a suffix; the grouped set of slices is { (key value, value_1), (key value_1, value_2), (key value_n-1, value_n), (key value_n, value) }, the implementation of step S3 is further specifically that the value of the dis data cache is sliced according to a preset value, so that the single value is prevented from being too large to cause the single timeout of the query, the grouping is in a manner of key: value_1, key_1:value_2, key_2:value_3, and the grouped set of slices is { (key value, value_1), value_2, value_n),. Key_1:value_n, wherein N is an integer greater than 1.
And S4, providing a redis reading interface, reading rediss the value of the data cache key, if the value is the value of which the end of the value is used for indicating that the redis data cache is segmented, continuing to read the data of the key value_1 of the next data segment, and so on, reading until the last segment, and splicing the characters of all the segments and returning to the client.
The implementation manner of the step S4 is further specifically that a redis reading interface is provided, a value of rediss data cache keys is read through the redis reading interface, if the value is a value ending with a value of 1 to indicate that the redis data cache is fragmented, data of the next data fragment key value_1 is continuously read, and the value corresponding to the data fragment key value_1 is ending with a value of 2, the data of the next data fragment key value_2 is continuously read, namely, a tail suffix of the value is used as an index to search for the same tail suffix of the key corresponding to the next data fragment, and if no suffix of the last value of the data fragment is regarded as being read for all fragmented data, the value of 1+value_2+value_N+value is spliced, so that complete data is obtained and returned to the client.
The invention is further described with reference to the following specific examples:
A method for detecting and fragmenting a value of a redis data cache, the method comprising the steps of:
Setting a threshold value of 100ms of the slow query of the redis, calculating the redis reading time through the time difference of the dotting of the logs before and after the redis reading, and asynchronously writing the redis key of the slow query into the log if the time difference exceeds the threshold value of 100 ms.
Setting a threshold of redis slow queries to 100ms, queries exceeding 100ms are likely to be caused by too large value. By means of the time difference between dotting before and after redis reading, the reading time of the redis is calculated, if the reading time exceeds a threshold value of 100ms, the redis key of slow query is asynchronously written into a log, and the mode of synchronously writing the log is not adopted in order to not reduce the throughput of the system, for example, the redis key of query is user xxx, and the recorded log content is user xxx'.
Step two, monitoring a slow query log, reading a redis data cache key of the slow query, skipping if the value of the redis data cache is smaller than 10KB, and deleting if the value of the redis data cache is larger than 100 KB.
Monitoring a slow query log, and reading the log content to obtain a user: xxx this reids key query is over 100ms, read user: the redisvalue of xxx, yielding a value content of' aaabbbccc. Then 10KB is 3 x 10 x 1024=30720 characters and skipped if value is less than 30720 characters. If the value is greater than 307200 characters, the redis key is deleted, because an oversized key would have a serious impact on the system, and the key is not stored.
Step three, if redis value is between 10-100KB, then the fragments are carried out according to a group of 10KB, each group of value is added with the key suffix of the next fragment as the end, the value of the 1 st group of fragments is expressed as the key_1 at the end of_1, the vaule of the second group of fragments is expressed as the end of_2, and the like from_3 to_9 until the last fragment is not added with the suffix.
If the redis value is between 10 and 100KB, namely 30720 and 307200 characters, the value is stored in a slicing way, so that the problem that a single value is too large to cause single overtime of inquiry is avoided. With 10kb as a group, the value ' aaabbbccc is assumed to have 61443 characters, the first 30720 characters as a group, the first group of tiles keys as user: xxx, value= ' aaabbbccc is assumed to have a 1' key suffix of the next group of tiles, the next group is 30721-61440 characters, the key of the second group of tiles as user: xxx_1, value is "2", the third group is 61441-92160 characters, since the whole value of characters is 61443 only, the first group is divided into three groups at most, value= ' zzz ' since it is the last group, no suffix is added.
Summarizing rules as
key:value_1
key_1:value_2
key_2:value_3
key_3:value_4
key_4:value_5
key_5:value_6
key_6:value_7
key_7:value_8
key_8:value_9
key_9:value_10
key_N-1:value_N;
key_N:value。
And step four, providing a general redis reading interface, reading the value of the redis key, if the value is a large value indicating that the redis is segmented by the end of the_1, continuing to read the data of the next data segmentation key_1, and the like, pushing the last segmentation, and splicing the characters of all the segments to return to the client.
In order to be able to recognize and read the large value of the fragmented redis, a general redis reading interface is provided as an inlet, a redis key ' user is read, value 1= ' aaabbbccc..1 '. Judging that the end of the value1 indicates that the redis is the large value of the fragmented, continuing to read the next data fragment key= ' user: xxx_1', obtaining fragment data value2 as end of the value2, continuing to read the next fragment ' user: xxx_2' obtaining value 3= ' zzz ', and if no suffix is considered as reading all the fragment data, splicing value1+value2+value3 to obtain complete data ' aaabbbccc..zz ', and returning data organized by the client.
Referring to fig. 2, the present invention further provides a system for detecting and slicing a value of a redis data cache, where the system includes a redis reading time acquisition module, a value size detection module, a slicing module, and a splicing and combining module;
The redis reading time acquisition module is used for setting a threshold value of the slow query of the redis data cache, acquiring the redis reading time through the time difference of log records before and after the redis data cache is read, and asynchronously writing keys of the slowly queried redis data cache into a log when the redis reading time exceeds the threshold value;
The value detection module monitors a slow query log, reads the key of the redis data cache of the slow query, reads the value corresponding to the key of the redis data cache, does not operate if the value is smaller than 10KB, ends the flow, deletes the key of the redis data cache if the value is larger than 100KB, and enters the fragmentation module if the value is between 10 and 100 KB;
The slicing module performs slicing according to groups of values of the redis data cache according to a preset value, wherein the content of each group of values is value_1, value_2 and value_3, and the like until the last slicing is not added with a suffix; the grouped set of tiles is { (key value, value_1), (key value_1, value_2) } (key value_n-1, value_n), (key value_n, value) };
And the splicing combination module provides a redis reading interface, reads the value of the rediss data cache key, continuously reads the data of the key value_1 of the next data fragment if the value is the value of which the end of the value is 1 and indicates that the redis data cache is fragmented, and similarly, pushes the last fragment to splice all the fragmented characters and returns the characters to the client.
The implementation mode of the redis reading time acquisition module is further specifically that a threshold value of a redis slow query is set to be 100ms, the query exceeding 100ms is the slow query caused by too large value, the redis reading time is obtained through the time difference of log records before and after the redis reading, if the threshold value exceeds 100ms, the redis data cache key of the slow query is asynchronously written into a log, and the mode of synchronously writing into the log is not adopted, so that the throughput of a system is not reduced.
The implementation mode of the slicing module is further specifically that the value of the redis data cache is sliced according to a preset value, so that the situation that a single value is too large to cause single time-out of a query is avoided, the grouping mode is that the value is key_1, value_2, key_2, value_3, key_N-1, value_N, value is an integer larger than 1, and the sliced set after grouping is { (key value, value_1), (key value_1, value_2) } (key value_N-1, value_N, value) }.
The implementation mode of the splicing combination module is further specifically that a redis reading interface is provided, a value of rediss data cache keys is read through the redis reading interface, if the value is a value which ends with a value of 1 to indicate that the redis data cache is fragmented, data of a next data fragment key value_1 is continuously read, and the value corresponding to the data fragment key value_1 ends with a value of 2, the data of the next data fragment key value_2 is continuously read, namely, the tail suffix of the value is used as an index to search for the same tail suffix of the key corresponding to the next data fragment, and if the last value of the data fragment does not have a suffix, the last value of the data fragment is regarded as being read to all fragmented data, and then the splicing value_1+value_2+ value_N+value is obtained, and complete data is returned to the client.
The foregoing description is only of the preferred embodiments of the invention, and all changes and modifications that come within the meaning and range of equivalency of the claims are therefore intended to be embraced therein.