Use oscahe caching technology to reduce frequent interactions with the database

I didn't know the specific implementation of the cache before, but I only knew that the data was stored in the memory, so that it can be read directly from the memory next time. There is no concept of the use of cache, and I feel that cache technology is a relatively "mysterious and unfamiliar" field. But recently, the cache technology is used, and it is found that it is necessary to find out.

 

Background of the use of caching technology: Generally speaking, for web projects, if we want any data to directly check the jdbc database, but in the case of high concurrency, it is impossible to check the database every time, because this is in high concurrency. It doesn't make sense in the case of - frequent interaction with the database is not a good thing, except in the case of not a lot of concurrency or no concurrency. We can find a solution: that is to check the database for the first time, cache the queried data in memory (use the key to specify the data), and next time we come to this step, judge whether the query conditions are the same, the same If it is, take it directly from the memory; if it is not the same, go to the library and cache the result for the next use...

The advantage of doing this is to reduce the interaction with the database. .

 

Let's start exploring using oscahe caching techniques. Here I used the oscache.jar package, decompiled the jar, and found a core class of cache management, GeneralCacheAdministrator.java, which is responsible for creating, destroying, and operating cache objects. This class extends a base class AbstractCacheAdministrator, which has the following basic properties:

 

// cached memory
public static final String CACHE_MEMORY_KEY = "cache.memory";
// Cache capacity (number of key-value pairs that can be accommodated)
public static final String CACHE_CAPACITY_KEY = "cache.capacity";
//cached algorithm
  public static final String CACHE_ALGORITHM_KEY = "cache.algorithm";

 

 

See some key methods in the GeneralCacheAdministrator class :

 

// get cache
public Cache getCache()
  {
    return this.applicationCache;
  }

// remove content from cache
  public void removeEntry(String key)
  {
    getCache().removeEntry(key);
  }
// get content from cache
  public Object getFromCache(String key)
    throws NeedsRefreshException
  {
    return getCache().getFromCache(key);
// cancel cache update
public void cancelUpdate(String key)
  {
    getCache().cancelUpdate(key);
  }
// refresh the cache
public void flushEntry(String key)
  {
    getCache().flushEntry(key);
  }
// refresh a group of caches
  public void flushGroup(String group)
  {
    getCache().flushGroup(group);
  }

……

 

There are many methods, but these methods are more commonly used, and it is easier to understand their functions from the perspective of code. 

 

How to use oscahe cache in project?

 

In order to be able to use oscahe better in web projects, first configuration is essential:

Look further at the constructor of the GeneralCacheAdministrator.java class:

 

private Cache applicationCache = null;
public GeneralCacheAdministrator()
  {
    this(null);
  }

  public GeneralCacheAdministrator(Properties p)
  {
    super(p);
    log.info("Constructed GeneralCacheAdministrator()");
    createCache();
  }

 public Cache getCache()
  {
    return this.applicationCache;
  }

private void createCache()
  {
    log.info("Creating new cache");

    this.applicationCache = new Cache(isMemoryCaching(), isUnlimitedDiskCache(), isOverflowPersistence(), isBlocking(), this.algorithmClass, this.cacheCapacity);

    configureStandardListeners(this.applicationCache);
  }

 We must have chosen the parameterized construct, since there are some necessary properties to specify. . Through the createCache() method, we can know that to construct a cache object, some basic properties can be specified.

 

 

<bean id="sysCacheOscache"
		class="com.opensymphony.oscache.general.GeneralCacheAdministrator"
		scope="singleton" destroy-method="destroy">
		<constructor-arg index="0">
			<props>
				<prop key="cache.memory">true</prop>
				<prop key="cache.capacity">600</prop><!-- Maximum number of cache elements-->
				<!-- The cache element exceeds the maximum value and is removed by the first int first out algorithm -->
				<prop key="cache.algorithm">com.opensymphony.oscache.base.algorithm.FIFOCache
				</prop>
			</props>
		</constructor-arg>
	</bean>

 

 

 

Notice:

1) When the database data is updated, don't forget to call the flush cache method;

2) Cached key setting problem:

- When you need to get a set, the cached key can be represented by a constant (select * from);

——When you go to the database to find different results according to different conditions (select xxx, xx, xx... from xx where ...), you need to pay attention that the key cannot be fixed with a constant, otherwise when you search the database for the first time, it will be Cache the results in memory, and when your query conditions change, the original results should change (recheck the database..., at this time the data structure is the query result as the map, and the combination of the query conditions as the key)

3) Don't forget that when the database data changes, the cache also needs to call flushEntry to refresh the content in the cache to avoid the content in the cache not being synchronized with the data in time. After the call, it will recheck the library and save the new results in the cache.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326892106&siteId=291194637