Summary of the invention
In view of the deficiencies in the prior art, it is slow that the purpose of the present invention is to provide a kind of data for having informing functionMethod is deposited, not only may insure that the object for only having an instantiation in cache pool exists, thus realize the shared of cache pool data,And when the caching or deletion for there are data in cache pool, related side can be notified at any time, monitors, economizes on resources without poll, led toKnow in time.
To achieve the above objectives, the technical solution adopted by the present invention is that:
A kind of data cache method having informing function, comprising steps of
Cache pool of the building one with informing function, and one is constructed for obtaining obtaining for cache pool object in the cache poolFunction is taken, one is defined in the acquisition function and is used for the constructed fuction of cache pool object-instantiated;
The variable whether an expression cache pool instantiates is defined in cache pool in advance, when needing to obtain cache poolWhen object, first judge whether cache pool object has instantiated according to the value of the variable, if so, directly acquiring instantiationCache pool object changes the value of the variable if it is not, then calling the constructed fuction by the cache pool object-instantiated, thenObtain the cache pool object of instantiation;
When the data stored in the cache pool object of instantiation change, the related side that need to be informed is notified.
Based on the above technical solution, it before the related side that notice need to inform, further comprises the steps of:
One is defined in cache pool object for storing the collection object of related side's information, and constructs one in the collectionThe registration function for increasing related side in object and one is closed for removing the anti-registration letter of related side in the collection objectNumber;
When related side, which needs to be apprised of the data stored in cache pool object, whether to change, then the registration is calledFunction increases to the related side in collection object, when whether related side does not need to be apprised of the data that store in cache pool objectWhen changing, then the anti-registration function is called to remove related side from collection object.
Based on the above technical solution, comprising steps of a notification protocol is pre-defined, when the cache pool pair of instantiationWhen changing as the data of middle storage, related side is notified to carry out respective handling according to the notification protocol.
Based on the above technical solution, it further comprises the steps of: and defines a HashMap type in data buffer storage pondCaching warehouse, when needing to carry out data buffer storage, stored in the caching warehouse corresponding data name and with the dataThe corresponding data value of title, when being deleted, deleted in the caching warehouse corresponding data name and with it is describedThe corresponding data value of data name.
The present invention also provides a kind of storage mediums, are stored thereon with computer program, and the computer program is processedThe step of above method is realized when device executes.
The present invention also provides a kind of electronic equipment, including memory, processor and be stored on the memory andThe computer program run on the processor, the processor realize the step of the above method when executing the computer programSuddenly.
The present invention also provides a kind of data buffering systems for having informing function, the system comprises creation module, sentenceDisconnected module and notification module;
The creation module is used to construct one the cache pool with informing function, and constructs one for obtaining the cache poolThe acquisition function of middle cache pool object defines one in the acquisition function and is used for the construction letter of cache pool object-instantiatedNumber;
The judgment module in cache pool for defining a variable in advance, when needing to obtain cache pool object, firstJudge whether cache pool object has instantiated according to the value of the variable, if so, directly acquiring the cache pool pair of instantiationAs if it is not, then calling the constructed fuction by the cache pool object-instantiated, changing the value of the variable, then obtain exampleThe cache pool object of change;
The notification module is used for when the data stored in the cache pool object of instantiation change, and notice needs to informRelated side.
It based on the above technical solution, further include a setup module and processing module;
The setup module is used for before the related side that notice need to inform, one is defined in cache pool object for storingThe collection object of related side's information, and one is constructed for increasing registration function, the Yi Jiyi of related side in the collection objectFor removing the anti-registration function of related side in the collection object;
The processing module is used to need to be apprised of whether the data stored in cache pool object change as related sideWhen, then call the registration function to increase to the related side in collection object, when related side does not need to be apprised of cache pool pairWhen whether changing as the data of middle storage, then the anti-registration function is called to remove related side from collection object.
Based on the above technical solution, the system also includes a protocol modules, and the protocol module is for preparatoryA notification protocol is defined, it is logical according to the notification protocol when the data stored in the cache pool object of instantiation changeKnow that related side carries out respective handling.
It based on the above technical solution, further include a memory module, the memory module is used in data buffer storage pondThe middle caching warehouse for defining a HashMap type stores corresponding when needing to carry out data buffer storage in the caching warehouseData name and data value corresponding with the data name deleted in the caching warehouse when being deletedCorresponding data name and data value corresponding with the data name.
Compared with the prior art, the advantages of the present invention are as follows:
(1) data cache method for having informing function of the invention not only may insure only have a reality in cache poolThe object of exampleization exists, thus realize the shared of cache pool data, and when the caching or deletion for having data in cache pool, it canRelated side is notified at any time, monitors, economizes on resources without poll, and notice is timely.
(2) data cache method for having informing function of the invention, related side can be according to whether monitoring datasVariation increase in collection object or deleted from collection object, when the cache pool data in cache pool object becomeWhen change, the related side in collection object is notified, to realize the control to related side, implementation method is simple.
(3) data cache method for having informing function of the invention, defines caching warehouse in data buffer storage pond, when needWhen carrying out data buffer storage, corresponding data name and data value corresponding with data name are stored in caching warehouse, when needWhen being deleted, corresponding data name and data value corresponding with data name are deleted in caching warehouse, to realizeThe storage and deletion of data, realization rate is simple, and extracts as needed to data convenient for subsequent.
Specific embodiment
Invention is further described in detail with reference to the accompanying drawings and embodiments.
Shown in Figure 1, the embodiment of the present invention provides a kind of data cache method for having informing function, comprising steps of
S1: cache pool of the building one with informing function, and one is constructed for obtaining obtaining for cache pool object in cache poolFunction is taken, one is defined in obtaining function and is used for the constructed fuction of cache pool object-instantiated.The specific implementation process is as follows:
Construction one is used to obtain the acquisition function of cache pool object in cache pool in cache pool, is defined as follows shown:
public SCDataManager getDataCacheInstance();
The function name of the acquisition function is getDataCacheInstance, printenv information, return value in the functionIt is the cache pool object of a SCDataManager type.And the constructed fuction for defining SCDataManager instantiation is newSCDataManager()。
S2: defining a variable in cache pool in advance, and the variable is for expressing whether the cache pool instantiates, when needWhen obtaining cache pool object, first judge whether cache pool object has instantiated according to the value of variable, if so, directly acquiringThe cache pool object of instantiation changes the value of variable if it is not, then calling constructed fuction by cache pool object-instantiated, then obtainsThe cache pool object of instantiation.
That is, the variable count of int (integer) type is defined in cache pool in advance, and the count variable is initialValue is assigned a value of 1, when the value of count variable is 1, then it represents that have and can only have a cache pool object in cache pool, i.e.,It can only once be instantiated.In the present embodiment, when the value of count variable is 1, illustrate that cache pool object not yet carries out realityExampleization.
And array SCDataManager [count] object is defined in SCDataManager class, it is deposited in the objectStorage be SCDataManager type object array, and by the number in the array be set as definition count value numberAmount.
How to be realized inside getDataCacheInstance function and finally returns that a SCDataManager objectProcess are as follows: whether the value for first determining whether count is to show that cache pool object is uninstantiated if being equal to 1 equal to 1, just logicalThe constructed fuction new SCDataManager () of SCDataManager is crossed to instantiate one new SCDataManager pairsAs, and the operation for subtracting one is carried out to the value of count after the completion of instantiation, SCDataManager pairs finally will just generatedAs being returned, that is, get the cache pool object of latest instance.If the value of count is equal to 0, generation is directly returnedSCDataManager object directly acquires the cache pool object of instantiation, without instantiating again again through constructed fuctionOne new object, so as to ensure that the object for only having an instantiation in cache pool exists, to realize cache pool dataIt is shared.
In addition to this, similarly, count variable initial value can also be assigned a value of 0, indicates slow when count variate-value is 0It deposits pond object not yet to be instantiated, and after instantiation, add to the value of count one operation, when count variate-valueIndicate that cache pool object has been instantiated, and after having instantiated, when needing to obtain cache pool object, is directly read when being 1The cache pool object of instantiation.Or other any modes, as long as can judge that cache pool object is according to the value of variable countIt is no to have instantiated.
S3: when the data stored in the cache pool object of instantiation change, the related side that need to be informed is notified.At thisIn embodiment, related side should be understood to need to inform the whether changed side of storing data in cache pool object, that is, needThe side for wanting monitoring data to change.
The data cache method for having informing function in embodiments of the present invention further comprises the steps of: pre-defined notice associationIt is corresponding to notify that related side carries out according to notification protocol when the data stored in the cache pool object of instantiation change for viewProcessing.
Specific notification protocol is as follows:
Before the related side that notice need to inform, further comprise the steps of:
One is defined in cache pool object for storing the collection object of related side's information, and constructs one in set pairAs the middle registration function for increasing related side and one in collection object for removing the anti-registration function of related side;
When related side, which needs to be apprised of the data stored in cache pool object, whether to change, then registration function is calledThe related side is increased in collection object, when related side does not need to be apprised of whether the data stored in cache pool object occurWhen variation, then anti-registration function is called to remove related side from collection object, to realize the control to related side, realization sideMethod is simple.
Specifically, defined in SCDataManager class one for store the set List of related side's information <Notification protocol defined above is stored in INotificaDataChanged > object listAll, collection object listAllContent and each related side's information.
In inventive embodiments, registration function in collection object for increasing related side, and anti-registration function is for collectingIt closes in object and removes related side.
Specific registration function is defined as follows:
public void registerData(INotificaDataChanged a);
The function name of registration function are as follows: the parameter in registerData, registerData function is oneThe object a of INotificaDataChanged type.
When whether a certain related side needs to be apprised of the data stored in cache pool object change, then needing shouldRelated side increases in collection object.Determine whether listAll is sky first, if it is the empty construction letter for then passing through registration functionNumber is by the example, if being not sky, is in listAll set by calling listAll.contains (a) method to determineIt is no to have contained the related side, if having contained the related side, do not have to continue to add the correlation into collection objectFang Liao, if not including the related side, by calling the add method in listAll that object a is added to collection objectIn listAll, the registration of related side can be completed, which is increased in collection object.
Specific anti-registration function is defined as follows:
publiv void unRegisterData(INotificaDataChanged b);
The function name of anti-registration function are as follows: the parameter in unRegisterData, unRegisterData function is oneThe object b of a INotificaDataChanged type.
When whether a certain related side no longer needs to be apprised of the data stored in cache pool object change, then needThe related side is removed from collection object.First determine whether listAll whether be it is empty, if it is it goes without doing any processing empty just,If being not sky, need to determine whether to be not necessarily to do any processing if not including the related side comprising the related side, if packetContaining the related side, then need to call remove (b) method in listAll to remove the related side from collection object, fromAnd the anti-registration of related side is completed, which is removed from collection object.
The data cache method for having informing function in embodiments of the present invention is further comprised the steps of: in data buffer storage Chi ZhongdingThe caching warehouse of an adopted HashMap type stores corresponding data name when needing to carry out data buffer storage in caching warehouseClaim and data value corresponding with data name, when being deleted, deleted in caching warehouse corresponding data name andData value corresponding with data name, to realize the storage and deletion of data, realization rate is simple, and convenient for subsequent according to needData are extracted.
Specifically: defined in SCDataManager class the caching warehouse HashMap of a HashMap type <String, Object > dataPool, the key in the HashMap object are the objects of a String type, and value is oneThe object of Object type.
Since the value of caching warehouse to be arranged to the object of Object type in embodiments of the present invention, Object object existsIt can be used to carrying any object in Java, it can for receiving the data of any object.Simultaneously as HashMapIt is the storage that data are carried out by key-value pair, key is the object of a String type, can also be understood to Object" alias " of object, or can be regarded as the title of data.
When needing to carry out data buffer storage, can determine whether caching warehouse dataPool is sky first, if dataPoolBe it is empty, then need to instantiate the caching warehouse object, by calling the constructed fuction of HashMap to carry out reality to itExampleization.After dataPool object is instantiated out, by call dataPool object in put (key,Value) for method by a target cache into caching warehouse, key herein is data name, and value is data value, and everyThe corresponding data name of a data value, the two have corresponding relationship.
If the capped situation of data may be will appear since key value is the same, the key value of different objects is wantedEnsure different.In order to ensure the uniqueness of key value, the definition of key is all placed in SCDataManager class and is determinedJustice, and uniqueness, such as key1, key2, key3 etc. at the end of key plus a number to ensure key, to realizeData buffer storage.
When needing data to delete, it is also desirable to first carry out empty judgement to dataPool object and illustrate buffering chamber if it is skyThere is no any data in library, at this time without doing any delete operation;If being not sky, needed at this time by calling dataPool pairsRemove (key) method as in deletes data, and wherein key is when data store using the data name arrived, whenWhen needing to delete certain data, will data name corresponding with the data value be passed in remove (key) function can be by the numberIt is deleted according to from caching warehouse.
In embodiments of the present invention, in data buffer storage, the side by traversal is needed in put (key, value) functionMethod traverses collection object listAll, and adjusts back onDataCached (the String key, Object in listAll setValue) method, by message to all related sides in listAll, key therein is the key of buffered data,Value is buffered value;Similarly, when data are deleted, pass through traversal readjustment in remove (key) functionOnDataRemoved (String key) method in listAll set adjusts back message to all correlations in listAllSide.The process may be implemented when data change, as long as registered related side can receive data changeMessage, to achieve the purpose that data Notification of Changes.
Whether the data cache method for having informing function of the invention judge cache pool object by the value of variableInstantiation, if so, the cache pool object of instantiation is directly acquired, if it is not, then calling constructed fuction by cache pool object instanceChange, change the value of variable, then obtain the cache pool object of instantiation, so as to ensure to only have an instantiation in cache poolObject exists, to realize the shared of cache pool data;And the cache pool has informing function, stores when in cache pool objectData when being stored or being deleted, that is, when the data stored change, inform related side by sending message, as long as thusData variation then notifies related side at any time, monitors, economizes on resources without poll, and notice is timely.
The embodiment of the invention also provides a kind of storage mediums, are stored thereon with computer program, and computer program is locatedManage the step of realizing the above method when device executes.
Shown in Figure 2, the embodiment of the invention also provides a kind of electronic equipment, including memory, processor and storageThe computer program run on a memory and on a processor, processor realize the step of the above method when executing computer programSuddenly.
The embodiment of the invention also provides a kind of data buffering system for having informing function, system include creation module,Judgment module and notification module;
Creation module is used to construct one the cache pool with informing function, and constructs one for obtaining cache pool in cache poolThe acquisition function of object defines one in obtaining function and is used for the constructed fuction of cache pool object-instantiated;
Judgment module in cache pool for defining a variable, when needing to obtain cache pool object, first basis in advanceThe value of variable judges whether cache pool object has instantiated, if so, directly acquire the cache pool object of instantiation, if it is not,It then calls constructed fuction by cache pool object-instantiated, changes the value of variable, then obtain the cache pool object of instantiation;
Notification module is used for when the data stored in the cache pool object of instantiation change, and notifies the phase that need to be informedGuan Fang.
Further, the data buffering system for having informing function further includes a setup module and processing module;
Setup module is used for before the related side that notice need to inform, one is defined in cache pool object for storing correlationThe collection object of square information, and one is constructed for increasing the registration function and one of related side in collection object for collectingClose the anti-registration function that related side is removed in object;
Processing module is used for when whether related side needs to be apprised of the data stored in cache pool object and change, thenIt calls the registration function to increase to the related side in collection object, is stored in cache pool object when related side does not need to be apprised ofWhen whether data change, then anti-registration function is called to remove related side from collection object.
The processing module of data buffering system can be according to whether the variation of monitoring data comes in the embodiment of the present inventionIncrease in collection object or deleted from collection object, when the cache pool data in cache pool object change, is led toKnow the related side in collection object, to realize the control to related side, implementation method is simple.
Further, the data buffering system for having informing function further includes a protocol module, and protocol module is for preparatoryA notification protocol is defined, when the data stored in the cache pool object of instantiation change, phase is notified according to notification protocolGuan Fang carries out respective handling.
Further, the data buffering system for having informing function further includes a memory module, and memory module is used in numberIt is stored in caching warehouse according to the caching warehouse for defining a HashMap type in cache pool when needing to carry out data buffer storageCorresponding data name and data value corresponding with data name are deleted corresponding when being deleted in caching warehouseData name and data value corresponding with data name, to realize the storage and deletion of data, realization rate is simple, and justData are extracted as needed in subsequent.
Whether the data buffering system for having informing function of the invention judge cache pool object by the value of variableInstantiation, if so, the cache pool object of instantiation is directly acquired, if it is not, then calling constructed fuction by cache pool object instanceChange, change the value of variable, then obtain the cache pool object of instantiation, so as to ensure to only have an instantiation in cache poolObject exists, to realize the shared of cache pool data;And the cache pool has informing function, stores when in cache pool objectData when being stored or being deleted, that is, when the data stored change, inform related side by sending message, as long as thusData variation then notifies related side at any time, monitors, economizes on resources without poll, and notice is timely.
The present invention is not limited to the above-described embodiments, for those skilled in the art, is not departing fromUnder the premise of the principle of the invention, several improvements and modifications can also be made, these improvements and modifications are also considered as protection of the inventionWithin the scope of.The content being not described in detail in this specification belongs to the prior art well known to professional and technical personnel in the field.