Several cached configuration _spring-data-jpa in SPRING DATA JPA

Source: Internet
Author: User
In the process of using spring data JPA, the use of Ehcache to do caching, whether the need for level two cache, generally do not need to see the business needs, because this thing if the configuration is not good, but will lead to performance degradation, but if it is some data, basically do not change, long-term unchanged, rarely modified, And the amount of data is moderate, and the external use frequency is high, still can use. In the current study of spring data JPA, there are three scenarios where it is possible to use the cache
1. Spring data JPA's own approach
2. Ways to extend the spring data JPA
3. Use Entitymanager to write your own cache of statements.

The implementation of the way, basically as follows, using code to express
Public interface Userdao extends crudrepository,usercustomer{
/**
* Caching of the first case, processing spring data J PA's Own method
* @param ID
* @return
/@QueryHints ({@QueryHint (name = "Org.hibernat    E.cacheable ", value =" true "}) public
User FindByID (long id);
/**
* A second scenario cache that handles methods that can be extended based on the spring data jpa.
* @return
/
@Query ("from User")
@QueryHints ({@QueryHint (name = "Org.hibernate.cacheable", VA    Lue = "true")})
List findallcached ();
}


If you are caching your own statements, use the following method
public class Userdaoimpl implements Usercustomer {
@PersistenceContext
private Entitymanager em;<     c3/>
/**
* A third type of caching, which is handled in a sethint manner.
*/Public
User Findbycustomizeid () {return
(User) em.createquery (' from User p where p.id=1 ')
. Sethint ("Org.hibernate.cacheable", True). Getsingleresult ();
}


Of course on the entity class also to note that the use of caching
@Cache (Usage = cacheconcurrencystrategy.read_only,region= "Mycache")
@Cacheable (True)
@Entity
@ The Table (name= "Xuser") public
class User {
...
}

As for the Ehcache configuration is very simple several lines of XML configuration, such as



In these cases, the test, once cached, does not see the issuing of the SQL statement, indicating that the cache is actually used.

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.