• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
13 caching   latest
 

13 caching latest

on

  • 566 views

 

Statistics

Views

Total Views
566
Views on SlideShare
566
Embed Views
0

Actions

Likes
0
Downloads
14
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    13 caching   latest 13 caching latest Presentation Transcript

    • Professional Open Source™ Caching© JBoss, Inc. 2003, 2004. 07/17/04 1
    • Caching Scopes Professional Open Source™  Transaction scope cache—Attached to the current unit of work, which may be a database transaction or even a conversation. It’s valid and used only as long as the unit of work runs. Every unit of work has its own cache. Data in this cache isn’t accessed concurrently.  Process scope cache—Shared between many (possibly concurrent) units of work or transactions. This means that data in the process scope cache is accessed by concurrently running threads, obviously with implications on transaction isolation.  Cluster scope cache—Shared between multiple processes on the same machine or between multiple machines in a cluster. Here, network communication is an important point worth consideration.© JBoss, Inc. 2003, 2004. 2
    • Professional Open Source™  Hibernate has a two-level cache architecture:  The first-level cache is the persistence context cache. This is at the unit-of-work level.  It corresponds to one session in Hibernate for a single request and is by default enabled for the Hibernate session.  The second-level cache is either at the process scope or the cluster scope. This is the cache of the state of the persistence entities.  A cache-concurrency strategy defines the transaction isolation details for a particular item of data, and the cache provider represents the physical cache implementation.© JBoss, Inc. 2003, 2004. 3
    • Using the Second-Level Cache in Hibernate Professional Open Source™  The cache policy involves setting the following: – Whether the second-level cache is enabled – The Hibernate concurrency strategy – Cache expiration policies (such as timeout, least recently used, and memory sensitive) – The physical format of the cache (memory, indexed files, or cluster-replicated)© JBoss, Inc. 2003, 2004. 4
    • Concurrency Strategies Professional Open Source™ – Transactional: This strategy should be used when there is more data to read. It prevents stale data in concurrency strategies. It’s equivalent to isolation level: repeatable read. – Read-write: This maintains a read-committed isolation level. – Non-strict read-write: This doesn’t guarantee that you won’t read stale data. It doesn’t guarantee consistency between cache and database. – Read-only: This is appropriate for data that never changes.© JBoss, Inc. 2003, 2004. 5
    • Cache Providers Professional Open Source™© JBoss, Inc. 2003, 2004. 6
    • Professional Open Source™  Not every cache provider is compatible with every concurrency strategies . The compatibility matrix is given in below Table :© JBoss, Inc. 2003, 2004. 7
    • Cache Regions Professional Open Source™  Cache regions are handles by which you can reference classes and collections in the cache provider configuration and set the expiration policies applicable to that region.  Regions are buckets of data of two types:  one type contains disassembled data of entity instances,  the other contains only identifiers of entities that are linked through a collection.  The name of the region is the class name in the case of a class cache or the class name together with the property name in the case of a collection cache.© JBoss, Inc. 2003, 2004. 8
    • Caching Query Results Professional Open Source™  A query’s result set can be configured to be cached. By default, caching is disabled, and every HQL, JPA QL, and Criteria query hits the database.  You enable the query cache as follows:  hibernate.cache.use_query_cache = true  In addition to setting this configuration property, you should use the org.hibernate.Query interface:  Query bookQuery = session.createQuery("from Book");  bookQuery.setCacheable(true);  The setCacheable() method enables the result to be cached.© JBoss, Inc. 2003, 2004. 9
    • First Level Cache Professional Open Source™  If you inspect the SQL statements executed by Hibernate, you find that only one database query is made.  That means Hibernate is caching your objects in the same session. This kind of caching is called first-level caching, and its caching scope is a session.© JBoss, Inc. 2003, 2004. 10
    • Professional Open Source™  Two database queries are made. That means Hibernate isn’t caching the persistent objects across different sessions by default.  You need to turn on this second-level caching, whose caching scope is a session factory.© JBoss, Inc. 2003, 2004. 11
    • Professional Open Source™ CONFIGURING SECOND LEVEL CACHE© JBoss, Inc. 2003, 2004. 12
    • Configuring cache provider Professional Open Source™  To turn on the second-level cache, the first step is to choose a cache provider in hibernate.cfg.xml.© JBoss, Inc. 2003, 2004. 13
    • Professional Open Source™  You can configure EHCache through the configuration file ehcache.xml, located in the source root folder.© JBoss, Inc. 2003, 2004. 14
    • Professional Open Source™  To monitor Hibernate’s caching activities at runtime, add the following line to the log4j configuration file log4j.properties:© JBoss, Inc. 2003, 2004. 15
    • Professional Open Source™  When you enable a class with the second-level cache, Hibernate doesn’t store the actual instances in cache.  Instead it caches the individual properties of that object.  For example, the instance of Book isn’t cached—rather, the properties in the book object like name and price are cached.  The cached objects are stored in a region that has the same name as the persistent class, such as com.training.Book.© JBoss, Inc. 2003, 2004. 16
    • Configuring cache usage strategy Professional Open Source™© JBoss, Inc. 2003, 2004. 17
    • Professional Open Source™  When the Book class is cached, only one SQL query is made, even though the book object is loaded  more than once in two different sessions. Because the second call to the database is avoided, the query’s performance improves:© JBoss, Inc. 2003, 2004. 18
    • Professional Open Source™ If you modify the book object in one session and flush the changes, an exception will be thrown for updating a read-only object.© JBoss, Inc. 2003, 2004. 19
    • Professional Open Source™  So, for an updatable object, you should choose another cache usage: read-write.  Hibernate invalidates the object from cache before it’s updated:© JBoss, Inc. 2003, 2004. 20
    • Professional Open Source™  You can invalidate either one instance or all instances of a persistent class:  factory.evict(Book.class);  factory.evict(Book.class, id);  factory.evictEntity("com.training.Book");  factory.evictEntity("com.training.Book", id);© JBoss, Inc. 2003, 2004. 21
    • Professional Open Source™  CacheMode options are provided to control the interaction of Hibernate with the second level cache:© JBoss, Inc. 2003, 2004. 22
    • CacheModes Professional Open Source™  CacheMode.IGNORE: Hibernate doesn’t interact with the second- level cache. When entities cached in the second-level cache are updated, Hibernate invalidates them.  • CacheMode.GET: Hibernate only reads and doesn’t add items to the second-level cache. When entities cached in the second-level cache are updated, Hibernate invalidates them.  CacheMode.PUT: Hibernate only adds and doesn’t add read from the second-level cache. When entities cached in the second-level cache are updated, Hibernate invalidates them.  • CacheMode.REFRESH: Hibernate only adds and doesn’t read from the second-level cache. In this mode, the setting of hibernate.cache.use_minimal_puts is bypassed,  as the refresh is forced.© JBoss, Inc. 2003, 2004. 23
    • Professional Open Source™ CACHING ASSOCIATIONS© JBoss, Inc. 2003, 2004. 24
    • Professional Open Source™  Is a book’s associated publisher object cached when its parent book object is cached?© JBoss, Inc. 2003, 2004. 25
    • Professional Open Source™ If you initialize the association in two different sessions, it’s loaded as many times as the initialization is made. This is because Hibernate caches only the identifier of publisher in Book’s region© JBoss, Inc. 2003, 2004. 26
    • Professional Open Source™  To cache the publisher objects in their own region, you need to enable caching for the Publisher class. You can do it the same way as for the Book class.  The cached publisher objects are stored in a region with the name com.training.bookshop.Publisher:  <hibernate-mapping package="com.training.bookshop">  <class name="Publisher" table="PUBLISHER">  <cache usage="read-write" />  ...  </class>  </hibernate-mapping>© JBoss, Inc. 2003, 2004. 27
    • Caching Collections Professional Open Source™ factory.evictCollection("com.training.Book.chapters"); factory.evictCollection("com.training.Book.chapters", id);© JBoss, Inc. 2003, 2004. 28
    • Caching Queries Professional Open Source™  By default, the HQL queries aren’t cached. You must first enable the query cache in the Hibernate configuration file:© JBoss, Inc. 2003, 2004. 29
    • Professional Open Source™  The setting cache.use_query_cache creates two cache regions: – one holding the cached query result sets – the other holding timestamps of the more recent updates to queryable tables.  By default, the queries aren’t cached. In addition to using the previous setting, to enable caching,  you need to call Query.setCacheable(true).  This allows the query to look for existing cache results or add  the results of the query to the cache.  The query result is cached in a  region named org.hibernate.cache.QueryCache by default.© JBoss, Inc. 2003, 2004. 30