Author: Xu jianxiang (netpirate@gmail.com)
Time: 2007-07-18
From: http://www.anymobile.org
Frequently used data is usually cached to improve efficiency. For large volumes of data, you can use Cache + index mechanisms for processing slice and information. For relatively simple data, you can use a simple data container for caching. Of course, they are generally stored in Hashtable or HashMap containers.
The principle of the Cache application is roughly as follows: the original data is imported into the Cache during initialization or it is loaded with inertia; it is often queried (with a high click rate) before it is set; when the total size of the data exceeds the Cache capacity, adjust the capacity or clear data that is not frequently used (with low click rate). If the data exceeds the validity period, clear the data in time. CHECK the data that fails to be queried.
A simple data container saves the data to be cached to a separate container and synchronizes the data under certain conditions. There are roughly two types of Synchronization Methods: Enable a Time Thread, regularly and centrally check and process the periodic synchronization operations of each container; or each container independently maintains its own periodic and synchronization operations.
The former performs unified processing without additional overhead. The disadvantage is that you need to add a new container to the synchronization thread. The latter processes the container separately, which is fast and without any constraints, they do not affect each other, but they have additional processing and object overhead.
Of course, independent processing does not mean that all business logic is controlled and executed in each container. You can use the proxy mechanism to solve this problem.
Static Proxy: both the proxy object and the object to be proxy must implement the same interface. services such as CHECK can be implemented in the proxy object and the proxy object can be called as needed, in this way, the agent only retains the business-related responsibilities.
Dynamic Proxy: later than JDK1.3, dynamic proxy is supported. The Handler class implements the java. lang. ref. InvocationHandler class, and one Handler can be used to serve various objects.
The dynamic proxy is relatively less efficient, and the collection static proxy mechanism. The data container process is as follows:
All data containers implement the iner interface, which defines the lifecycle, timestamp, synchronization method, and Data Reading method. defines a static proxy class. When querying data, check whether the data container has expired. If the container has expired, the container synchronization method is called.
The class diagram is as follows:
The sequence diagram is as follows:
The sample code is as follows:
Container container = new StaticProxy (EntityContainer. getInstance ());
Int [] arr = (int []) container. getOne ("025 ");
If you understand AOP, you can easily think of pointcut, advisor, MethodBeforeAdviced, and so on. The principle is similar, just a little.
(Full text)