Embodiment
Such as the application scenarios of a data access of Fig. 1 examples, user's data to be accessed are stored with database 11, are used
Family can be by terminals such as PC, mobile devices, initiation data inquiry request, certain data in acquisition request database 11.Number
It can be handled according to inquiry request by the request reception device 12 of example in Fig. 1, the request reception device 12 can be by database 11
The data of request are obtained, and return to request end.For example, the device can be in an equipment, receive and sent out in the equipment
The multiple data inquiry requests risen;Or can also be in a plateform system, for receiving the number from multiple equipment
According to inquiry request.
For faster response data inquiry request, " local cache " technology can be generally used, local cache is i.e. at this
A part of space is marked off in the physical memory on ground and is used for buffered data, to improve response efficiency.For example, the request in Fig. 1 connects
Receiving apparatus 12 can go database 11 to obtain number when receiving the inquiry request for certain data first by a thread
According to and by the data storage in local cache, during in case receiving the request to same data again, directly from local acquisition
Return, can so significantly improve the response efficiency to inquiry request.It is corresponding with local cache, the number of actual storage data
Physical layer data is properly termed as according to storehouse 11, physical layer data 11 is the space of data persistence storage, for example, it may be data
Storehouse.
In some scenes, because data are constantly to update, so the data being stored in local cache there are
Imitate the time.Within effective time, request reception device 12 can obtain data directly from caching and return to request end;But work as
When the data of local cache are expired, request reception device 12 needs to go physical layer data such as database 11 to obtain new number again
According to update local cache.
It during data storage, is stored in cache blocks one by one in local cache to ask reception device 12,
Each cache blocks can be a part of space of internal memory, and different cache blocks can store different pieces of information.It is slow with one
Exemplified by counterfoil, the attribute of cache blocks can include:
Timer:Timer, the generation time for record buffer memory block;
Living Mill Seconds:The life span of cache blocks, in units of millisecond, in the life span, caching
Data in block are effective, more than life span, it is necessary to obtain data renewal cache blocks by physical layer data;
Object:The data object stored in cache blocks.
Above three attribute, it is the attribute that a cache blocks generally have.In a follow-up exemplary field of the application
Jing Zhong, in order to realize data query method described herein, the attribute of a cache blocks is added, specifically by follow-up example
Described in son.
In the prior art, in the data query scene of above-mentioned Fig. 1 examples, it is possible that a kind of phenomenon, i.e., " caching is hit
Wear ".Such as, it is assumed that there is an a data, the data in local cache are just expired, it is necessary to refresh, and now come just
Largely the high frequency access queries of the data to be asked, then these a large amount of requests can all go physical layer data to obtain new data,
The pressure moment of physical layer data is caused to rise violently, this phenomenon is referred to as caching breakdown.
It is not only the caching that above-mentioned data cached time-out is likely encountered when concurrently accessing to puncture, accesses certain data first
When it is equally possible run into, such as, it is assumed that largely inquire about same data request reach request reception device 12, then request connect
The request of each thread process one in receiving apparatus 12, local cache not no data during due to accessing first, these threads are all
Database 11 can be gone to inquire about, cause the pressure moment of database 11 to rise violently, this is also a kind of caching punch-through.
Caching punch-through is larger by the pressure for causing physical layer data, and great prestige is formed to the stability of physical layer
The side of body, a kind of data query method that the application provides, has been the above-mentioned caching breakdown problem of solution, to ensure physical layer data
It is no longer influenced by the instantaneous pressure of mass data request.
First, the application describes lower the application's by taking above-mentioned " accessing first " and " caching time-out " two scenes as an example
How method avoids caching from puncturing under both scenes;Certainly, both scenes are not limited in specific implementation, other classes
As may cause caching puncture scene, this method can be used.
Access first:
In this example, it is assumed that the data storage in local cache, be that hash table is a kind of basis to hash sheet form storage
The data structure that key value directly conducts interviews, Hash table is also, form of the data in hash table is key-value.Should
In example, when inquiring about data, key can be carried in data inquiry request, the data of inquiry be the key corresponding to value, can
So that the value inquired about is referred to as into target data.
Data query flow shown in Figure 2, following processing can be performed when accessing first:
In step 201, data inquiry request is received, key is carried in the request.
The request received in this step, it is therefore an objective to which inquiry obtains value corresponding to key.
In step 202, check in local cache, cache blocks corresponding to key, if for sky.
In this example, each key can correspond to a cache blocks, and the cache blocks are used to store value corresponding to key.
Request reception device 12 first checks whether to deposit the data asked in step 201 in local cache.Because this example is to this
The access first of data, so the cache blocks in the absence of the data.
In this step, the empty cache blocks of an acquiescence can be set, delayed as corresponding to the key asked when accessing first
Counterfoil, it is now no data in the cache blocks, is sky.Otherwise, if cache blocks corresponding to a key in local cache
It is not sky, then it is not the access first to data, will be described in other examples, this example is only described as branch's flow of sky
(handling process when accessing first).
In step 203, attempt to pass through putIfAbsent methods, it is incoming to set value corresponding to key in cache blocks
Value.
PutIfAbsent is a method in hash table, there is provided a kind of atom ability:Attempt to set a key-value pair
(key-value) when, if data (value) are existing corresponding to key (key), it is not provided with, directly returns and be worth;It is no
Then, data corresponding to key are arranged to incoming value, the incoming value can preset a data.
In this example, it is assumed that currently encountered and the substantial amounts of of same data is concurrently accessed, substantial amounts of inquiry request is same
When will obtain same data, and the data still access first.In this case, ask reception device 12 will be right respectively
Each data inquiry request uses a thread process, and each thread in multiple threads performs shown in Fig. 2 respectively
Flow, each thread can judge that this is to access first, and cache blocks are sky, also, each thread will be tasted respectively
It is incoming value to ping putIfAbsent methods and set value corresponding to key in cache blocks.
Assuming that one of thread is set successfully, i.e. this thread by putIfAbsent methods when being set, caching
Value there is not yet corresponding to key in block, so value is set to incoming value by the thread;But when this thread is arranged to
After work(, other threads all by setup failed because, other threads again by putIfAbsent methods attempt set when, cache blocks
Value corresponding to middle key is present (the incoming value that thread i.e. above is set), then, other threads are just no longer set, and
It is that direct return has been worth.
It can be seen that when multiple threads concurrently set same cache blocks data by putIfAbsent methods, only
One can set success, and other threads all can setup failed.For single thread, the different knots of success or failure are set
Fruit, different processing will be performed.Continuing with referring to Fig. 2, if configured to work(, continues executing with step 204 to 205, if set
Failure, will continue executing with step 206.
In step 204, the target data is inquired about to physical layer data.
If thread putIfAbsent is set successfully, show to there is currently no the concurrently inquiry to target data, here
Mean that before the thread not having other threads also goes physical layer data to inquire about same data, this thread is most fast line
Journey.So, the thread can calls the inquiry service of registration, removes value corresponding to physical layer data inquiry key, and can be with
Obtain the when effective life span of data.
In step 205, caching is stored data in.
This step can be by the data storage inquired about from physical layer data in local cache, and the existence of record buffer memory block
Time, next time can be by directly returning in local cache.
The data got also are returned to inquiry request side by this step.
In step 206, the current data in the cache blocks is returned as the target data.
If thread putIfAbsent setup faileds, show to there is currently the concurrently inquiry to target data, the i.e. thread
Before setting, there are other threads to go physical layer data to inquire about same data, now, the thread immediately returns to, and will be slow
Current data in counterfoil as returned data, current data be before the incoming value that is set by putIfAbsent of thread.
It can see by the access process first of Fig. 2 flows, it is a large amount of concurrent when accessing same data first simultaneously, can be with
These threads accessed of processing are set all to go putIfAbsent that same cache blocks are set, putIfAbsent methods are equivalent to one
Kind tournament selection mechanism, a large amount of threads only have one can set success, go physical layer data to inquire about data;Also, according to
The characteristics of putIfAbsent methods, if after a thread is set successfully, other threads all can setup failed, so as to set only
Allow that successful thread to go to perform physical layer data inquiry, effectively prevent caching breakdown when accessing first.
Caching time-out:
This application scene can be that the step 202 in Fig. 2 is judging cache blocks not for branch's flow of space-time, wherein, such as
During fruit data query, the cache blocks of local cache are not sky, then this is not the access first to data.In this case, can be with
Handled according to the data query flow shown in Fig. 3:
In step 301, data inquiry request is received, key is carried in the request.
The request received in this step, it is therefore an objective to which inquiry obtains value corresponding to key.
In step 302, check in local cache, cache blocks corresponding to key, if for sky.
In this example, it is assumed that cache blocks are not sky, then perform step 303.
In step 303, judge whether cache blocks are expired.
As mentioned above, each cache blocks has a life span attribute, and the data in cache blocks are in life span
Just effectively, physical layer data is needed to obtain the data after renewal when expired.
The judgement of this step, can be according to life span living the Mill Seconds, Yi Jiji in caching block's attribute
When device timer, to determine whether life span overtime.If have not timed out, then the data of the cache blocks are still effective, perform
Step 304;If had timed, out, step 305 is continued executing with.
In step 304, the current data in return cache block is as target data.
In step 305, judge whether cache blocks are updating.
In this example, also include in the attribute of the cache blocks:Update mark retrieving.The update mark includes two
Kind state value:Very (true) and false (false), the true respresentation current cache block updates the data, i.e., current wired
Journey goes physical layer data to obtain latest data, and the false expression current cache block does not update the data, although that is, cache blocks are
Through expired, but there has been no thread to go physical layer data to obtain new data.
, can be according to above-mentioned update mark retrieving state when this step judges whether cache blocks update
Value, if state value is true, updates, otherwise, if state value is false, does not update.
If a determination be made that cache blocks update, then step 304 is performed, that is, has had thread to go physical layer data to obtain
Latest data is taken to update the cache blocks, so current thread is returned to, without going physical layer data to inquire about identical again
Data.
If a determination be made that cache blocks not yet update, then step 306 is performed.
Within step 306, by without lock synchronous method, attempting to set the state of the update mark in the cache blocks
Value is true.
For example, one kind can be CAS (Compare and Swap, relatively and exchange) without lock synchronous method.In this example,
CAS operation can, if the update mark state value in the cache blocks is false, the state value is changed into true,
It is now to set successfully;Otherwise, if the update mark state value in the cache blocks is not false, no longer set, this
When represent setup failed.
In this step, it is assumed that it is expired in cache blocks and when not yet updating, currently encounter substantial amounts of concurrently access and all inquired about
The data of the cache blocks, then according to Fig. 3 flow, multiple threads corresponding to these inquiry requests can all judge that cache blocks need
Update, it is necessary to go physical layer data to obtain latest data, according to the flow of this example, above-mentioned multiple threads will all be attempted respectively
The state value that the update mark of cache blocks is set by CAS methods is true.But only one of which thread is set successfully,
That is, this thread by CAS methods when being set, the false that judges in the state value or step 305 of update mark, so
The thread can set success, be set to true;But after this thread is set successfully, other threads pass through CAS again
When method is attempted to set, because the state value of update mark has been modified into true, then setup failed.
It can be seen that when multiple threads concurrently set the update mark of same cache blocks by CAS methods, only one
Individual to set success, other threads all can setup failed.For single thread, the different knots of success or failure are set
Fruit, different processing will be performed.Continuing with referring to Fig. 3, if configured to work(, continues executing with step 307 to 309, if set
Failure, will continue executing with step 304, data expired in cache blocks can be returned into inquiry request side.
In step 307, target data is inquired about to physical layer data.
In step 308, local cache is stored data in.
Value corresponding to the key inquired can be updated data expired in cache blocks in this step, and will
Data return to inquiry request side.
In a step 309, it is false to recover the update mark.
In this example, after data are got by physical layer data, the state value of update mark is also reverted into false,
Represent that current cache blocks no longer have updated.
It can see by Fig. 3 cache blocks timeout inquiries flow, largely concurrently inquiry is attempted to same cache blocks simultaneously
Data renewal when, threads update mark for all going CAS to set the cache blocks of these inquiries of processing, CAS methods can be set
A kind of tournament selection mechanism is also corresponded to, a large amount of threads only have one can set success, go physical layer data to inquire about data, and
And as long as a thread sets successfully after, other threads all can setup failed, only allow that successful thread to go so as to set
Physical layer data inquiry is performed, effectively prevent caching breakdown when time-out refreshes.
In addition, still by taking Fig. 1 scene as an example, the data inquiry request received by reception device 12 is asked, it may be possible to first
The request of secondary access, it is also possible to which the flow of the request under cache blocks timeout case, i.e. Fig. 2 and Fig. 3 is all likely encountered, then should
Device can integrate unified Fig. 2 and Fig. 3 processing, after receiving inquiry request, first judge whether is cache blocks in local cache
Then it is to access first if sky for sky, can be according to Fig. 2 flow processings, if being not sky, right and wrong access first, can be with
According to Fig. 3 flow processings.
From the point of view of the example of " accessing first " described above and " caching time-out " two scenes, whether
PutIfAbsent, or CAS, the two methods have a feature, it would be possible to cause a large amount of concurrent requests of caching breakdown
Each thread in thread, go perform one operation, this operation the characteristics of be that only a thread is operable to work(,
Other threads are by operation failure;And it is just to operate successfully during in the absence of concurrent thread, operation failure when concurrent thread be present
(for example be because having there is thread to perform operation in advance if putIfAbsent fails;Or if CAS fails,
That is to have other threads to perform setting).According to These characteristics, it is believed that the operation performed by above-mentioned thread is one
Kind of " concurrently judge operation ", according to the result of operation it is known that current with the presence or absence of concurrently inquiry, (for example if CAS loses
Lose, then it represents that exist concurrent), and then decide whether to allow thread to go physical layer data to inquire about.
In other application scenarios, it can also be other behaviour outside putIfAbsen or CAS concurrently to judge operation
Make, as long as above-mentioned operating function can be realized, the application concurrently judges to operate also with thread is performed into this, to determine
With the presence or absence of concurrent, and then thread is controlled not go physical layer data to inquire about data in the presence of concurrent.Fig. 4 illustrates the application's
Data query method, it can include:
In step 401, data inquiry request is received, the data inquiry request, which is used to inquire about, obtains target data.This
The data that step will inquire about are referred to as target data, for example, value corresponding to key.
In step 402, target data is inquired about in local cache, when it is determined that needing to obtain number of targets to physical layer data
According to when, to corresponding to target data described in local cache cache blocks perform concurrently judge operate.
In this example, if target data in local cache be present, then get data by local cache and return to and look into
Requesting party is ask, is largely concurrently inquired about even if existing, and by locally responding, physical layer data will not be had an impact.
But when the target data is not present in local cache, at this moment just needs physical layer data inquiry to obtain data, just have
It is likely encountered foregoing caching breakdown problem.Here " it is determined that needing to obtain target data to physical layer data ", for example, can be with
Cache blocks when accessing first for sky, so as to need to obtain data to physical layer data, or can also right and wrong access first
When cache blocks time-out cause data failure, it is necessary to physical layer data obtain renewal data.
In step 403, if operating result shows to there is currently no the concurrently inquiry to the target data, to data
Physical layer inquires about the target data;Otherwise, the current data in the cache blocks is returned as the target data.
The data query method of this example, concurrently judge to operate by performing when receiving inquiry request, it may be determined that
It is current concurrent with the presence or absence of inquiry, so as in the absence of it is concurrent when just inquire about data to physical layer data, effectively prevent appearance
Cache the phenomenon of breakdown.
In order to realize the above method, present invention also provides a kind of data query arrangement, as shown in figure 5, the device can be with
Including:Request receiving module 51, buffer process module 52 and data acquisition module 53.
Request receiving module 51, for receiving data inquiry request, the data inquiry request, which is used to inquire about, obtains target
Data;
Buffer process module 52, for inquiring about the target data in local cache, when it is determined that needing to data entity
When layer obtains the target data, cache blocks corresponding to target data described in local cache are performed and concurrently judge to operate;
Data acquisition module 53, if showing to there is currently no the concurrently inquiry to the target data for operating result,
Then the target data is inquired about to physical layer data;Otherwise, the current data in the cache blocks is returned as the number of targets
According to.
In one example, it is described to determine to need to obtain the target data to physical layer data, be specially:The data
Inquiry request is the inquiry first to target data, and cache blocks corresponding to the target data are sky.
In one example, the local cache uses key-value storage form, and the data inquiry request carries
Key, the target data are value corresponding to the key;
The buffer process module 52, when concurrently judging operation for performing, including:In caching corresponding to the key
In block, attempt to set value corresponding to the key by putIfAbsent as incoming value;
The data acquisition module 53, specifically for when the buffer process module is set successfully, then showing currently not
In the presence of the concurrently inquiry to the target data, the target data is inquired about to physical layer data;Otherwise, in return cache block
Current value corresponding to key, as the target data.
In one example, it is described to determine to need to obtain the target data to physical layer data, be specially:Local cache
Described in cache blocks corresponding to target data it is expired, and the cache blocks not yet update.
In one example, the attribute of the cache blocks includes:Update mark, the update mark include two states
Value:True and false, the true respresentation current cache block update the data, and the false expression current cache block does not update the data;
The buffer process module 52, when concurrently judging operation for performing, including:By without lock synchronous method, tasting
It is true that examination, which sets the state value of the update mark in the cache blocks,;
The data acquisition module 53, specifically for when the buffer process module is set successfully, then showing currently not
In the presence of the concurrently inquiry to the target data, and after the target data success is inquired about to physical layer data, described in recovery
Update mark is false.
The device or module that above-described embodiment illustrates, can specifically be realized by computer chip or entity, or by with
The product of certain function is realized.One kind typically realizes that equipment is computer, and the concrete form of computer can be personal meter
Calculation machine, laptop computer, cell phone, camera phone, smart phone, personal digital assistant, media player, navigation are set
It is any several in standby, E-mail receiver/send equipment, game console, tablet PC, wearable device or these equipment
The combination of equipment.
For convenience of description, it is divided into various modules during description apparatus above with function to describe respectively.Certainly, this is being implemented
The function of each module can be realized in same or multiple softwares and/or hardware when open.
The preferred embodiment of the disclosure is the foregoing is only, not limiting the disclosure, all essences in the disclosure
God any modification, equivalent substitution and improvements done etc., should be included within the scope of disclosure protection with principle.