[Logo] Terracotta Discussion Forums (LEGACY READ-ONLY ARCHIVE)
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Confusion regarding key,value in cache  XML
Forum Index -> Ehcache
Author Message
satya087

journeyman

Joined: 10/14/2010 05:58:35
Messages: 16
Offline

Hi,
i studied ehcache documentation, and i came accross if we use
annotation
@Cacheable(cacheName="cacheName" ) before a method then the values the method returns gets stored in cache whose name is provided in the annotation. It also said that if the method has a parameter then the parameter name becomes the key.

eg-
@Cacheable(cacheName="studentCache" )
getStudent(String studentName)
{
.....
}

here student name becomes the key for all the values the method returns.

how does this automatic key assigning works when there are
1)No parameters

eg-
@Cacheable(cacheName="studentCache" )
getStudent()
{
//returns all the students
.....
}



2)Multiple Parameters

eg-
@Cacheable(cacheName="studentCache" )
getStudent(String studentName,int marks)
{
//returns selected students
.....
}


Kindly suggest if this methodology is appropriate for all cases.
gluck

qaestor

Joined: 09/15/2009 18:01:23
Messages: 179
Location: Brisbane, Australia
Offline

Hi this is actually a question for http://code.google.com/p/ehcache-spring-annotations/

I will ping Eric Dalquist with your question.
satya087

journeyman

Joined: 10/14/2010 05:58:35
Messages: 16
Offline

thank you, please let me know the solution asap
edalquis

journeyman

Joined: 02/10/2010 10:44:27
Messages: 11
Offline

For an overview of how cache key generation works you can take a look at: http://code.google.com/p/ehcache-spring-annotations/wiki/CacheKeyGenerators

For your specific case, assuming you haven't overridden the default cache key generator in the spring context the HashCodeCacheKeyGenerator is used. It its default configuration the Class, Method, and method arguments type array is part of the cache key as well as the actual argument values.

So for your example:
Code:
class MyDao {
   @Cacheable(cacheName="studentCache" )
   public Student getStudent(String studentName);
 }


What the key generator does is build an array of {Class, Method, {String.class}, {"studentNameValue"}}. The values in that array are, in order:
-The Class object is the MyDao class
-The Method object for the getStudent method
-A Class[] of the parameter types
-An Object[] of the parameter values

This array is then passed into the hashing code which does a deep traversal of the arrays aggregating the hash codes of the objects into a single value.

Your second example, with a method that has no arguments the generated array would look like: {Class, Method, {}, {}} So a valid cache key based on just the class and method name is created.

Your third example, with a method that has two arguments the generated array would look like: {Class, Method, {String.class, Integer.class}, {"studentNameValue", 42}}

StringCacheKeyGenerator generates keys that are human readable and may be a little more obvious when inspecting the cache as to where the key has come from.

All of the built in key generators can be configured to exclude the method signature (which excludes both the parent Class and Method objects) via the includeMethod property which defaults to true. Also the parameter type Class[] can be excluded by the includeParameterTypes property which defaults to true.

Key generators can be configured on a per-annotation basis using the
@UsingKeyGenerator annotation.


I hope that answers your questions. Please feel free to subscribe to the project's user help email list at: http://groups.google.com/group/ehcache-spring-annotations
satya087

journeyman

Joined: 10/14/2010 05:58:35
Messages: 16
Offline

thank you very much for the answer.
rajoshi

seraphim

Joined: 07/04/2011 04:36:10
Messages: 1491
Offline

Issue seems to be resolved.Please let us know if more information is required.

Rakesh Joshi
Senior Consultant
Terracotta.
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team