Java Caching JSR107 (5)

Source: Internet
Author: User

Cache entry Listener events and Listener

CacheEntryEvent, an abstract class of cache events, is defined in JCache. And the event type EventType enumeration class, including four event types. For definitions, see:

Public enum EventType {

CREATED, // create

UPDATED, // update

REMOVED, // Delete

EXPIRED // EXPIRED

}

These events will be propagated to the CacheEntryListener registered in the Cache. This interface is a tag interface, and the Listener interface corresponding to the four event types inherits this interface, the parameters are CacheEntryCreatedListener, CacheEntryUpdatedListener, CacheEntryRemovedListener, and CacheEntryExpiredListener.

Listener Registration

The Listener is not necessarily in the Cache process. To avoid serialization instance registration, you need to use CacheEntryListenerConfigurations (specific class MutableCacheEntryListenerConfiguration ).

During configuration, developers can use the MutableConfiguation. addCacheEntryListenerConfiguration method to increase the Listener Configuration of the Cache, or register the Listener through the Cache. registerCacheEntryListener method at runtime. You can use the Cache. deregisterCacheEntryListener method to cancel registration.

Multiple CacheEntryListenerConfiguration can be added to Configuration. When the cache is initialized, it uses the registered Factory to create CacheEntryListener. Multiple Listener corresponding to the same or different eventtypes can exist for a cache. When an event is created or distributed in a Listener, the framework cannot guarantee its sequence.

Listener call

Cache Listener

● Triggered after the cache entry changes.

● In synchronization mode, a given key triggers the Listener in the order of the event and blocks the call thread until the Listener returns.

● In asynchronous mode, multiple events are traversed in an undefined order, but the events must be processed in the order of occurrence for the same Key.

Listener is in the Observer mode. When Listener throws an exception, the cache operation will not fail.

Modifying the cached value in a Listener may lead to deadlocks. detection and response deadlocks are specific to implementation.

The registered Listener in the cache implementation can be called at most once for each event.

A Listener is not necessarily executed in the event process. In a distributed environment, Listener can be implemented anywhere.

The Listener can have a CacheEntryEventFilter, Which is configured through CacheEntryListenerConfiguration.

Filter and the same Listener. It is not necessarily executed in the process where the event occurs. In a distributed environment, filters can be implemented on any node that provides the best performance.

The following table summarizes the Listener operations called by each cache operation. The condition is the status of the entry before the operation, and the expiration is always "no". The exact expiration time is based on the specific implementation of the cache.

Method

Create

Expired

Delete

Update

Boolean containsKey (K key)

No

No

No

No

V get (K key)

Yes. If it is created through read-through

No

No

No

Map GetAll (Collection Keys)

Yes. If it is created through read-through

No

No

No

V getAndPut (K key, V value)

Yes. If not

No

No

Yes. If yes

V getAndRemove (K key)

No

No

Yes. If yes

No

V getAndReplace (K key, V value)

No

No

No

Yes. If yes

T invoke (K key, EntryProcessor EntryProcessor );

Yes. If you call setValue () to create or call getValue () to create through read-through

No

Yes. If you call remove ()

Yes. If setValue () is called to update

Map InvokeAll (Set Keys,

EntryProcessor EntryProcessor, Object... arguments );

Yes. If you call setValue () to create or call getValue () to create through read-through

No

Yes. If you call remove ()

Yes. If setValue () is called to update

Iterator > Iterator ()

No

No

Yes. If you call remove ()

No

Void loadAll (Set Keys, boolean replaceExistingValues,

CompletionListener completionListener );

Yes. If not

No

No

Yes. If yes

Void put (K key, V value)

Yes. If not

No

No

Yes. If yes

Void putAll (Map Map)

Yes. If not

No

No

Yes. If yes

Boolean putIfAbsent (K key, V value)

Yes. If not

No

No

No

Boolean remove (K key)

No

No

Yes. If yes

No

Boolean remove (K key, V oldValue)

No

No

Yes. If yes and equal

No

Void removeAll ()

No

No

Yes. If yes

No

Void removeAll (Set Keys)

No

No

Yes. If yes

No

Boolean replace (K key, V value)

No

No

No

Yes. If yes

Boolean replace (K key, V oldValue, V newValue)

No

No

No

Yes. If yes and equal

Cache entry executor

A javax. cache. cache. entryProcessor is a callable function, just like a java. util. concurrent. callable, an application can use it to effectively perform atomic cache operations of a combination, including access, update, and cache entries without explicit locks or transactions.

For example, you may want to check the value of a cache entry, calculate a new value, update the entry, and return atomic operations for other values, an application can use the custom EntryProcessor to implement this function.

For the definition of javax. cache. Cache. EntryProcessor, see:

Public interface EntryProcessor {

/**

* Process an entry.

* @ Param entry: cache entry

* @ Param arguments: set of parameters processed

* @ Return indicates the processing result.

*/

T process (Cache. MutableEntry Entry, Object... arguments );

}

To call EntryProcessor on a Cache entry, the application must use the Cache. invoke method to call the processor of a single key and use Cache. invokeAll to call the processor of a key set.

The following example shows how to use EntryProcessor to automatically increase the value of a cache entry.

CachingProvider provider = Caching. getCachingProvider ();

CacheManager manager = provider. getCacheManager ();

MutableConfiguration Configuration =

New MutableConfiguration ()

. SetTypes (String. class, Integer. class );

Manager. createCache ("example", configuration );

Cache Cache = manager. getCache (

"Example", configuration );

String key = "counter ";

Cache. put (key, 1 );

Int previous = cache. invoke (key,

NewIncrementProcessor ());

Assert previous = 1;

Assert cache. get (key) = 2;

IncrementProcessor is defined as follows.

Public static class IncrementProcessor

Implements Cache. EntryProcessor {

@ Override

Public Integer process (Cache. MutableEntry Entry,

Object... arguments ){

If (entry. exists ()){

Integer current = entry. getValue ();

Entry. setValue (current + 1 );

Return current;

} Else {

Entry. setValue (0 );

Return-1;

}

}

}

Supports remote or distributed cache topology implementation. You can choose to execute the entry processor in a remote process. In this case, you may need to obtain EntryProcessors. The called parameters and return types can be java. lang. Serializable or serialized in some way. Another optional method is to implement simple serialization of the EntryProcessor class name, together with the specified call parameters, by remotely instantiating the EntryProcessor class and deserialization call parameters to execute remote call requests.

The output result of an EntryProcessor is atomic, so it can interact with the cache loader, cache writer, cache item listener, and expiration policy.

When an EntryProcessor is called, an application will never see the intermediate events or side effects of calling the MutableEntry getValue, etValue, and remove methods, however, you can only observe the final results of operations on Cache entries on one EntryProcessor.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.