Declarative Caching Services for Spring

Discussions

News: Declarative Caching Services for Spring

  1. Declarative Caching Services for Spring (13 messages)

    Dev2Dev has published Alex Ruiz' "Declarative Caching Services for Spring," which removes the need for programmatic usage of caching code in Spring. The caching service uses an interceptor to manage the cache, and the article shows configuration on an application and per-bean basis, along with information on integration testing and other issues caused by the use of a transparent cache. The caching proxy changes the use of a Coherence cache from this:public class CustomerManager { private CustomerDao customerDao; private CacheKeyGenerator keyGenerator; public Customer load(long customerId) { validateCustomerId(customerId); Customer customer = null; Serializable key = keyGenerator.generateKey(customerId); NamedCache cache = getCache(); customer = (Customer) cache.get(key); if (customer == null) { customer = customerDao.load(customerId); cache.put(key, customer); } return customer; } public void update(Customer customer) { customerDao.update(customer); Serializable key = keyGenerator.generateKey(customer.getId()); NamedCache cache = getCache(); cache.remove(key); } private NamedCache getCache() { return CacheFactory.getCache("customerCache"); } // rest of class implementation }With the use of the caching proxy:public class CustomerManager { private CustomerDao customerDao; public Customer load(long customerId) { validateCustomerId(customerId); return customerDao.load(customerId); } public void update(Customer customer) { customerDao.update(customer); } // rest of class implementation }
    Although caching can dramatically improve the performance of enterprise applications, it also can introduce considerable complexity to the application life cycle. This article introduces Declarative Caching Services for Spring, a framework that encapsulates such complexity and provides code-free, transparent caching and cache flushing to Spring-powered applications. In the spirit of Spring, declarative caching provides out-of-the-box support for various open source and commercial cache providers. It also offers different configuration strategies. It is up to us, the developers, to choose the combination that best suits the needs of our projects. With declarative caching, developers can concentrate on the core requirements of their applications which results in code that is easier to understand, maintain, test, and reuse. Because caching services are configured in the Spring IoC container, they can be added only when truly needed, without making system-wide changes.

    Threaded Messages (13)

  2. This is a easy caching method, but what can i do in a cluster environment?
  3. Just configuration in coherence[ Go to top ]

    Coherence, and most other cache providers, would have configurations determine whether the cache is a clustered/distributed cache or local cache. Assuming the cache itself was configured as a distributed cache, the above code would also work in a clustered environment and alert the other cache instances to the change.
  4. Hi Evan, You might find it interesting. GigaSpaces and Interface21 Extend the Simplicity of Spring to Stateful Low-Latency Applications. The joint solution consists of two parts: - Middleware abstraction – DAO, Transaction, Cache, JDBC, Remoting, JMS – Enabling a non-intrusive approach for implementing the business logic on top of GigaSpaces. - Service Abstraction – Enabling dynamic deployment of the Spring beans on the Grid. Milestone 2 of GigaSpaces’ Enterprise Edition that contains built in support for Spring services is available at: http://www.gigaspaces.com/os_downloads.html#f See also support for JavaSpaces/GigaSpaces at SpringModules 0.4: https://springmodules.dev.java.net/docs/reference/0.4/html_single/#javaspaces For Wiki documentation use http://www.gigaspaces.com/wiki/display/GS/Spring Regards, Gershon Diner GigaSpaces Technologies WRITE ONCE SCALE ANYWHERE
  5. the PR link...[ Go to top ]

    Evan, here is the PR link which I forgot to add :-) - See http://biz.yahoo.com/prnews/060615/ukw014.html?.v=57 Regards, Gershon
  6. This is a easy caching method, but what can i do in a cluster environment?
    The examples in the BEA article used Coherence, which is clusterd :-) Peace, Cameron Purdy Tangosol Coherence: The Java Data Grid
  7. update()[ Go to top ]

    public void update(Customer customer) {
    customerDao.update(customer);
    Serializable key = keyGenerator.generateKey(customer.getId());
    NamedCache cache = getCache();
    cache.remove(key);
    }
    Why is 'customer' removed from the cache after update?
  8. seems reasonable[ Go to top ]

    remove customer from cache because it's now stale in cache. When someone other or you load again same customer there is no stale customer object in cache and you get fresh one from database
  9. Re: seems reasonable[ Go to top ]

    remove customer from cache because it's now stale in cache. When someone other or you load again same customer there is no stale customer object in cache and you get fresh one from database
    The "fresh one from database" is the same (updated) object that already is in the cache.
  10. Re: update()[ Go to top ]

    public void update(Customer customer) {
    customerDao.update(customer);
    Serializable key = keyGenerator.generateKey(customer.getId());
    NamedCache cache = getCache();
    cache.remove(key);
    }

    Why is 'customer' removed from the cache after update?
    I think the whole idea is that you never have to do this again. Everything is handled in the interceptors. I have a question on updates though: In the MethodMapCachingInterceptor, etc. the entire cache is flushed. Why? Why can't we have an interceptor in the framework (of course I can write one) that removes or updates one entry only? I work for a company that purchased Coherence but does not use Hibernate, so I am using the Caching Module which I found very useful and eye-opening. Yet, I think if we used Hibernate, the Caching module would not be necessary and the effort would be spent (I do not know how much) on Coherence-Hibernate integration. Am I right?
  11. Re: update()[ Go to top ]

    In the "old-way" example, the cache key is generated from a hash of the customer-id, so the update method can flush the stale entry that would otherwise be read by the load method. In the new way example by Alex Ruiz with the AOP style caching, the cache key is generated by a hash of the "intercepted method" and the parameter. In this case, the load method will read the cache with key = hash of load(String customerId) and the update method will flush the entry with key = hash of update(Customer cust). So how does it work? Janos, I guess that may be the answer to your question "In the MethodMapCachingInterceptor, etc. the entire cache is flushed. Why? Why can't we have an interceptor in the framework (of course I can write one) that removes or updates one entry only? " With the hashkey generator, there is simply no way to correlate any particular entries.
  12. Certainly a good article to read. However, I think the declarative caching approach may apply more to entity caching. This is becuase the coarse-grained approach that the method-level interpception entails (e.g., in the article example of "load" and "update" where you have more clear cut of synchornization). But if it is used mostly in the entity bean case, then typical ORM library like Hibernate already handles the caching behind the scene. All a user needs to do is to declare the entity to be "cacheable" or not. Besides, issues like cache invalidation also needs to be taken into consideration when it is distributed. If the bean to be cached is of stateful nature, then I am not sure method-level caching declaration will be that concise either. For one, stateful bean usually has more fine-grained nature, e.g., pojo.getName and setName(). In addition, if there are persistency or replication involved, then you probably want to do it judiciously. For this, cache framework like PojoCache (PojoCache, a component of JBoss Cache) that actually handles distributed caching transparently can work pretty well. Cheers, -Ben Wang Lead PojoCache JBoss, Inc.
  13. Clustering with JCache[ Go to top ]

    With Essence you could cluster this object with the following code. Note: load and update could be dropped unless needed. // keep it simple! public class CustomerManager { private final Map customers; public CustomerManager(Map customers) { this.customers = customers; } public Customer load(long customerId) { return customers.get(customerId); } public void update(Customer customer) { customers.put(customer.getId(), customer); } } In the configuration you need a one line entry for the CustomerManager, the "customers" Cache and one for each host in the cluster. (four more lines) BTW: a JCache extends a Map so you should be able to this with any product which supports JCache and Java 5.
  14. I am one of the authors of a new project intended to provide Ehcache integration for Spring 3 projects via annotations:

    http://code.google.com/p/ehcache-spring-annotations/

    The library provides two method-level annotations in the spirit of Spring’s @Transactional:

    @Cacheable
    @TriggersRemove

    When appropriately configured in your Spring application, this project will create caching aspects at runtime around your @Cacheable annotated methods.

    Usage documentation can be found on the project wiki:

    http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheAnnotations
    http://code.google.com/p/ehcache-spring-annotations/wiki/UsingTriggersRemove