Oracle Coherence Chinese tutorial 23: Using continuous query caching

Source: Internet
Author: User

Using continuous query caching

While it is possible to obtain time from a coherent cache query result in a point, and it is possible to receive events that will change the results of the query, coherence provides a feature that combines query results with related events in a continuous stream of data in a real-time manner to keep an up-to-date query result. This ability is called continuous query because it has the same effect if you want to query for 0 latency and queries are executed multiple times per millisecond! For more information about time-of-day query results and events , see Chapter VI," querying data in the cache." "

Coherent materialized into a continuous query cache query results, and then, the cache keeps the latest real-time query using event listeners to implement continuous query function. In other words, a coherent continuous query is a cached query result that never expires.

This chapter contains the following sections:

The purpose of the continuous query cache
Continuous query Cache Coherence
Building a continuous query cache
Cache keys only, or cache keys and values
Listen to the Continuousquerycache.
Make continuousquerycache read-only

23.1using continuous query caching

There are several different types of continuous query caching general purpose categories:

It is an ideal building block for complex event processing (CEP ) systems and event correlation engines.

This is an ideal case where the application repeats a particular query and will benefit from always having instant access to the latest results.

Continuous query caching is similar to materialized views, and the results of a query using the standard namedcache API for accessing and manipulating, and receiving the event-related continuous flow of the query is very useful.

To approach caching in a similar way, you can use a continuous query cache because it keeps an up-to-date set of data that is being used locally, such as a node on a particular server or a desktop on a client computer ; note that near-cache is not valid, but it is actually the latest way to maintain its data in successive query caches.

An example of using the case is a trading system desktop, where a trader's open orders and all relevant information must always be kept in the latest way. The application can support several Ching concurrent users by combining the continuous query cache coherence * extension function.

Precautions:
Continuous query caching is very useful because they provide almost all types of applications, including the ability to base client-and server-based applications, to easily and efficiently maintain a specified subset to the most recent local copy of a larger, potentially distributed cache dataset.

23.2 Coherence Continuous query cache

Coherent implementation of continuous query found inCom.tangosol.net.cache.ContinuousQueryCacheclass. This class, like all coherent caches, implements the standardNamedcacheinterface, which includes the following features:

Cached access and operation using the map interface:Namedcacheextends the standardJavaThe set frame, and its interface is the sameJDKof theHashMapand theHashtableclass implements theMapinterface.

The modification of all active objects occurs in the cache:Namedcacheextension ofObservablemapinterface.

Identity-based cluster-wide locking of objects in the cache:Namedcache Concurrentmapinterface extension.

Querying objects in the cache:Namedcachean interface for extending the property map.

Objects in the distributed parallel processing and aggregation cache:NamedcacheExtension InterfaceInvocablemap.

Because of theContinuousquerycacheImplementNamedcachethe interface, which is the sameAPIprovided by all coherent caches, it is very simple to use and it can easily be replaced by another cache when it comes to functional requirements.

23.3building a continuous query cache

There are two items that define a continuous query cache:

It is based on the underlying cache ;

Generating subsets, underlying cache queries, continuous query cache caching.

The underlying cache is any consistent cache, including another continuous query cache. Caching is usually obtained by handing cachefactory , which allows the developer to specify only the name of the cache, which automatically configures the application-based cache configuration information, for example:
Namedcache cache = Cachefactory.getcache ("Orders");

See Appendix B , " caching configuration elements " For more information, specifying cache configuration information.

A query is a query that will be used for the same type, for example:

Example 23-1 query for continuous query caching

Filter filter = new Andfilter (New Equalsfilter ("Gettrader", Traderid),

New Equalsfilter ("GetStatus", Status.open));

See Chapter VI, " querying data in cache " for more information inquiries.

Typically, the query cache, an example of a property check method, gets a snapshot of all open trades for this transaction:

Example 23-2 fetching data in a continuous query cache

Set setopentrades = Cache.entryset (filter);

Similarly, those same two successive query cache constructs:

Example 23-3 Constructing a continuous query cache

Continuousquerycache cacheopentrades = new Continuousquerycache (cache, filter);

23.3.1cleanup of related resources andContinuousquerycache

The continuous query cache places one or more event listeners with its underlying cache. If the continuous query cache is used for the duration of the application and then cleans the resource, the node is shut down or otherwise stopped. However, if the continuous query cache is used only for a period and then used to complete the application, the application must call the release() method on continuousquerycache .

23.4cache keys only, or cache keys and values

When building a continuous query cache, it is possible to specify that the cache should only track the key from the query results and get the cached value only if they are required from the underlying. Creating a continuous query cache that represents a very large query result set may be useful if the value is never or rarely required. To specify that a unique key should be a cache, use the cachevalues allowed by the constructor ?? property to configure, for example:

Example 23-4 constructor allows cachevalues Property

Continuousquerycache cacheopentrades = new Continuousquerycache (cache, filter, false);

If necessary, can also be modified cachevalues?? after the property cache has been instantiated, for example:

Example 23-5 setting the cachevalues property

Cacheopentrades.setcachevalues (TRUE);

23.4.1 cachevaluesproperties and event listeners

If the continuous query cache has any standard (non-lite) event listeners, or if any event listeners are filtered, then the cachevalues?? property is automatically set to true because the continuous query cache uses the locally cached value to filter the event to supply the old and new values, it raises the event.

23.5 monitoring Continuousquerycache

Because the continuous query cache itself is observable, it is possible for the client to place one or more event listeners on it. For example:

Example 23-6 Adding a listener to the continuous query cache

Continuousquerycache cacheopentrades = new Continuousquerycache (cache, filter);

Cacheopentrades.addmaplistener (listener);

Assume that some processing occurs in cache and each item is added to the cache, and there are two methods for each item. First, the processing can occur and then add a listener to handle any later additions:

Example 23-7 to add a listener that handles successive query cache entries

Continuousquerycache cacheopentrades = new Continuousquerycache (cache, filter);

for (Iterator iter = Cacheopentrades.entryset (). Iterator (); Iter.hasnext ();)

{

Map.entry Entry = (map.entry) iter.next ();

// .. Process the cache entry

}

Cacheopentrades.addmaplistener (listener);

However, this code is not correct because it allows the iteration to be missed before the event that occurs in the blink of an hour is added to the previous listener! Another way is to add a listener first, so no events are missed and then processed:

Example 23-8 Adding a listener to process a continuous query cache entry

Continuousquerycache cacheopentrades = new Continuousquerycache (cache, filter);

Cacheopentrades.addmaplistener (listener);

for (Iterator iter = Cacheopentrades.entryset (). Iterator (); Iter.hasnext ();)

{

Map.entry Entry = (map.entry) iter.next ();

// .. Process the cache entry

}

However, the same entries can appear in the iterations of both events, and the events can be asynchronous, so the order of operations cannot be guaranteed.

The solution is to provide a listener during construction, which receives an event in the successive query cache for each item, whether it is there, because it is in the query at the beginning () or in the process or after it has been added, if the cache is built:

Example 23-9 Construction provides a listener when continuous query caching

Continuousquerycache cacheopentrades = new Continuousquerycache (cache, filter, listener);

23.5.1implement a stable materialized view

the Continuousquerycache implementation faces the same challenge: How to assemble a snapshot of the exact point-in-time, the underlying cache, and receive a modified stream of events from the same cache. There are several parts to the solution. First, Coherence supports an option for synchronous events, which provides a set of ordering guarantees. For more information on this option, see Chapter " using cache activity ".

Second, Continuousquerycache Its initial population, allowing the its first to query the underlying cache and then resolve all the events that were scheduled during the first phase with a two-phase implementation. Since the realization of these data visibility guarantees that no missing or duplicated events are quite complex, the continuousquerycache allows the developer to pass a listener during the construction process, This avoids the complexity of making these same application developers.

23.5.2support for both synchronous and asynchronous listeners

By default, listeners Continuousquerycache their activities asynchronously. However, the continuousquerycache respects the option of synchronizing events as provided by the Synchronouslistener interface. For more information on this option, see Chapter " using continuous query caching " .

23.6MakingContinuousquerycachethe read-only

Continuousquerycache can be made into a read-only cache, for example:

Example 23-10 continuous Query cache read-only

Cacheopentrades.setreadonly (TRUE);

Read-only Continuousquerycache objects are not allowed to be added, changed, deleted, or locked in the cache.

When a continuousquerycache has been set to read-only, it cannot be changed back to read / write.

Oracle Coherence Chinese tutorial 23: Using continuous query caching

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.