Alachisoft NCache 4.1 - Online Documentation

Step 2: Register NCache with Spring

Following are the steps required to enable NCache in Spring application:
Step 1: Add Library:
Add "NCacheSpringAnnotation.jar" library to your Spring application. This .jar file is placed at the following path:
After adding the library, changes are required in 2 different files:
Step 2: Spring Deployment Descriptor Configurations:
Spring Deployment Descriptor already exist in a spring application, following changes are required to be made in this file:
1. In order to inject NCache Spring Provider, you need to add the following two highlighted attributes in the existing schema:
  • Add xmlns:ncache attribute with its value in the schema.
  • Concatenate the NCache location in xsi:schemaLocation with the already existing location fields, as shown below:
[Modified Schema]
<beans xmlns=""
2. In Spring Deployment Descriptor, add the following element:
            <ncache:cache id="ID-1" name="mycache"/>
            <ncache:cache id="ID-2" name="myreplicatedcache"/>
"NCache:annotation-driven" tag provides the facility to add number of caches to be used with Spring framework application.
Step 3: Application Level Changes:
NCache is providing number of annotation to perform various cache related tasks. You only need to add those annotations on the top of your methods to perform those tasks.
1. Cacheable:
The purpose of this annotation is to cache the data return by the method with a specific key. Before caching the data, @cacheable generates a method specific key according to the given specifications and cache the data against that key. Here users are supposed to specify the cache id where he/she is interested to cache the data of the method. This annotation is applicable to the methods of a class.
For example:
public Collection<Message> findAllMessages()
    Collection<Message> values = messages.values();
    Set<Message> messages = new HashSet<Message>();
    synchronized (messages) {
            Iterator<Message> iterator = values.iterator();
            while (iterator.hasNext()) {
    return Collections.unmodifiableCollection(messages);

2. Triggers Remove:
As mentioned above, a specific key is required to cache the data of the method. This annotation follows the same procedure for removing the data of a method by generating a similar key with which it is added.  The purpose of @triggersremove annotation is to remove the data of a method from the cache by matching the key with the cached keys. To remove data from the cache you need to specify the following attributes:
  • CacheID: Specify the cache id from where the data is to be removed.
  • When: It will specify either data is removed before or after the execution of a method. It take two different values i.e. When.AFTER_METHOD_INVOCATION and When.BEFORE_METHOD_INVOCATION.
  • RemoveAll: If value of this attribute is "False", it will only remove data of a specific key. If value is "True", it will clear the cache and remove all class related data from the cache.
This annotation is applicable to the methods of a class.
For example:
            when = When.BEFORE_METHOD_INVOCATION,
            removeAll = false          
        public void updateMessage(long id, String to, String body)
                Message message = new Message();
                      messages.put(id, message);
You can cache values of two different methods of a class in two different caches.
Two similar annotations can't be used on top of same method.
You can not run @TriggersRemove for two caches on top of the same method.
    See Also
Copyright © 2005-2012 Alachisoft. All rights reserved.