EhCache comment (reprint)

In fact EhCache use is the Spring Cache comment.

@Cacheable 1.1
@Cacheable may be labeled in a way, it may also be marked on a class. It indicates that the method is to support the cache, when a mark indicates the class class methods are all supported in the cache when a tag method. For a way to support caching, Spring will return value cached after it is called, to ensure you can get the next time the method is performed using the same parameters directly from the cache As a result, without the need to execute the method again. Spring in the return value caching method is based on the return key caching, value is the result of the method, as key words, Spring and supports two strategies, the default policy and custom policy, this will be explained later. Note that when one supports caching method is called inside the object will not trigger a cache function. @Cacheable can specify three attributes, value, key and condition.

Cache 1.1.1 value attribute specifies the name
value property must be specified, which represents the return value is the current method will be cached on which the Cache, Cache corresponds to the name. Which may be one or may be a plurality of Cache Cache, when it is necessary to specify that it is a plurality of Cache array.

   @Cacheable("cache1")//Cache是发生在cache1上的
   public User find(Integer id) { return null; } @Cacheable({"cache1", "cache2"})//Cache是发生在cache1和cache2上的 public User find(Integer id) { return null; } 

1.1.2 key property custom key
key attribute is used to specify when the corresponding cache Spring key returns the result of the method. The property supports SpringEL expressions. When we do not specify this attribute, Spring will use the default strategy generated key. Here we take a look at the custom policy, as a default policy will be introduced separately later.
Custom policy means that we can specify our key by Spring EL expression. Here EL expression can use parameters and their corresponding attributes. When using the method parameter we can directly use the "# parameter name" or "#p parameter index". The following are some of the key parameters as an example.

  @Cacheable(value="users", key="#id")
  public User find(Integer id) { return null; } @Cacheable(value="users", key="#p0") public User find(Integer id) { return null; } @Cacheable(value="users", key="#user.id") public User find(User user) { return null; } @Cacheable(value="users", key="#p0.id") public User find(User user) { return null; } 

In addition to the above-described method parameters as the key, Spring also provides a root object us used to generate key. We can get the following information through the root object.

 
Paste_Image.png

When we want to use the root object's attributes as key we can "#root" omitted because the default Spring property is the root object of use. Such as:

  @Cacheable(value={"users", "xxx"}, key="caches[1].name")
  public User find(User user) { return null; } 

1.1.3 condition attribute specifies the condition occurs
sometimes we may not want to cache a method to return all of the results. This functionality can be achieved through the condition property. condition property defaults to blank, all calls will be cached situations. The value is specified by SpringEL expression that when the cache processing is true; represents not to cache, i.e., the method will be executed each time the method is called once when to false. Following example represents only when an even number of id user will be cached.

  @Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
  public User find(User user) { System.out.println("find user by user " + user); return user; } 

1.2 @CachePut
with the support of Spring Cache environment, marked for use @Cacheable, Spring whether there is a cache element in the same key checks before each execution Cache, if there is no longer performing the method, but directly from the cache get the results returned, otherwise it will execute and return the result into the specified cache.
@CachePut can also declare a method to support caching feature. @Cacheable except that the method is not marked @CachePut to check the results of whether or not there was performed before the cache prior to execution, but the method is performed every time, and (return value) of the execution result of key-value form into the specified cache. So there must be a return value, no return value will not be placed in a new cache.
@CachePut may be marked on the classes and methods. When using @CachePut we can specify the properties with @Cacheable is the same.

  @CachePut("users")//每次都会执行方法,并将结果存入指定的缓存中
  public User find(Integer id) { return user; } 

@CacheEvict 1.3
@CacheEvict is used to mark on the need to clear the cache element of the class or method. Which represents a method of performing all of the triggers cleanup when the cache tag in a class. @CacheEvict can specify attributes value, key, condition, allEntries and beforeInvocation. Wherein @Cacheable similar semantics corresponding attribute value, key, and the condition. That value represents the cleanup operation is (name of the corresponding Cache) which takes place on the Cache; key which expressed the need to clear the key, if not specified then the default key strategies generated; condition clears operating conditions occur. Here we introduce two properties allEntries and beforeInvocation emerging.
1.3.1 allEntries property
allEntries type boolean indicating whether all the elements need to clear the cache. The default is false, meaning not needed. When you specify allEntries is true, Spring Cache ignores the specified key. Sometimes we need to look at Cache to clear all of the elements, than a clear element of a more efficient.

  @CacheEvict(value="users", allEntries=true)
  public void delete(Integer id) { System.out.println("delete user by id: " + id); } 

1.3.2 beforeInvocation property
cleanup after the default is triggered by a corresponding method is successful, that method will not be triggered if the clearing operation failed because of an exception is thrown and returned. Use beforeInvocation to change the trigger cleanup time, when we specify the property value is true, Spring will clear the specified elements in the cache before calling the method.

  @CacheEvict(value="users", beforeInvocation=true)
  public void delete(Integer id) { System.out.println("delete user by id: " + id); } 

In fact, in addition to using @CacheEvict clear the cache elements, but when we use Ehcache as to achieve, we can also configure Ehcache get rid of their strategy, which is specified by Ehcache profile.
@Caching 1.4
@Caching annotation allows us to specify multiple annotations Spring Cache related in one way or class. It has three attributes: cacheable, put and evict, are used to specify @ Cacheable, @ CachePut and @CacheEvict.

   @Caching(cacheable = @Cacheable("users"), evict = { @CacheEvict("cache2"), @CacheEvict(value = "cache3", allEntries = **true**) }) **public** User find(Integer id) { **return****null**; } 

3.1 Key of the default policy
default key generation strategy by KeyGenerator is generated, the default strategy is as follows:

  • If the method has no parameters, 0 is used as a key.
  • If only one parameter, then this parameter is used as the key.
  • If more than one parameter, then use the hashCode all parameters as a key.

If we need to specify your own default policy, then we can implement your own KeyGenerator, and then specify KeyGenerator our Spring Cache used to KeyGenerator our own definition.
Annotation-based configuration used by cache: annotation-driven specified.

   <cache:annotation-driven key-generator=*"userKeyGenerator"*/>
  
   <bean id="userKeyGenerator" class="com.xxx.cache.UserKeyGenerator"/> 

When using XML-based configuration through cache: advice to specify.

   <cache:advice id="cacheAdvice" cache-manager="cacheManager" key-generator="userKeyGenerator"> </cache:advice> 

Important:
In fact EhCache use is the Spring Cache annotations should be noted that when one supports caching method is called inside the object will not trigger a cache function.
@Cacheable parameters: value means the cache on which Cache, key refers to the method return the cached results corresponding key, condition refers to the cache under what circumstances.
If it is found in the cache @Cacheable same key will fetch data from the cache. @CachePut re-run every time, and then the results into the cache. @CacheEvict clear the cache.
If no key. This is the default strategy is Key (parameters), you can also customize key generation strategy



Link: https: //www.jianshu.com/p/db110523a387
reference  http://www.iteye.com/blogs/subjects/ehcache

Guess you like

Origin www.cnblogs.com/guanbin-529/p/10927711.html