Announcement Announcement Module
Collapse
No announcement yet.
Configuring Cache in spring 3.1 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring Cache in spring 3.1

    Please find the below code. I would like to store person and persons in same cache place,
    ex: whenever i hit Summary page, the records will cached, and if i try to open a record i would like to get the data from previously stored cache not from database.but i tried with googling to store an collection of objects with key , but i can't get the solution.

    Please help me in doing this.
    Thanks in advance..

    package com.sravan.service;

    import java.util.Collection;

    import javax.annotation.Resource;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;

    import com.sravan.beans.Person;
    import com.sravan.dao.PersonDAO;
    @Service(value="personService")
    public class PersonServiceImpl implements PersonService{
    @Resource(name="personDao")
    private PersonDAO persondao;
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @CacheEvict(value="person",key="#person.id")
    public boolean deletePerson(Person person) {
    return this.persondao.deletePerson(person);
    }
    @Cacheable(value="person",key="#id")
    public Person findById(int id) {
    Person person=this.persondao.getPersonById(id);
    logger.debug("person" +person.getPersonAddresses());
    return person;
    }
    @Cacheable(value="persons")
    public Collection<Person> getAllPerson() {
    return this.persondao.getAllPerson();
    }

    @CacheEvict(value="persons",allEntries=true)
    public Person savePerson(Person person) {
    logger.debug("while saving person");
    if(this.persondao.saveOrUpdatePerson(person))
    return person;
    else return new Person();
    }
    @CachePut(value="person",key="#person.id")
    public Person updatePerson(Person person) {
    if(this.persondao.saveOrUpdatePerson(person))
    return person;
    else return new Person();
    }
    }

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable...

    Only adding the @Cache annotations isn't going to do anything you also need something to configure your cache. Also I'm not certain if it is possible to share this cache as both have different keys and store different things. It might be that, when you use for instance hibernate, enabling 2nd level caching on your ORM implemenation might make this work (this would mean you cannot use the @Cache annotation anymore but rely on your ORM provider).

    Comment

    Working...
    X