30
Professional Open Source™ © JBoss, Inc. 2003, 2004. 1 07/17/04 Caching

13 caching latest

Embed Size (px)

Citation preview

Page 1: 13 caching   latest

Professional Open Source™

© JBoss, Inc. 2003, 2004. 1

07/17/04

Caching

Page 2: 13 caching   latest

© JBoss, Inc. 2003, 2004. 2

Professional Open Source™

Caching Scopes

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.

Page 3: 13 caching   latest

© JBoss, Inc. 2003, 2004. 3

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.

Page 4: 13 caching   latest

© JBoss, Inc. 2003, 2004. 4

Professional Open Source™

Using the Second-Level Cache in Hibernate

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)

Page 5: 13 caching   latest

© JBoss, Inc. 2003, 2004. 5

Professional Open Source™

Concurrency Strategies

– 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.

Page 6: 13 caching   latest

© JBoss, Inc. 2003, 2004. 6

Professional Open Source™

Cache Providers

Page 7: 13 caching   latest

© JBoss, Inc. 2003, 2004. 7

Professional Open Source™

Not every cache provider is compatible with every concurrency strategies . The compatibility matrix is given in below Table :

Page 8: 13 caching   latest

© JBoss, Inc. 2003, 2004. 8

Professional Open Source™

Cache Regions

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.

Page 9: 13 caching   latest

© JBoss, Inc. 2003, 2004. 9

Professional Open Source™

Caching Query Results

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.

Page 10: 13 caching   latest

© JBoss, Inc. 2003, 2004. 10

Professional Open Source™

First Level Cache

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.

Page 11: 13 caching   latest

© JBoss, Inc. 2003, 2004. 11

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.

Page 12: 13 caching   latest

© JBoss, Inc. 2003, 2004. 12

Professional Open Source™

CONFIGURING SECOND LEVEL CACHE

Page 13: 13 caching   latest

© JBoss, Inc. 2003, 2004. 13

Professional Open Source™

Configuring cache provider

To turn on the second-level cache, the first step is to choose a cache provider in hibernate.cfg.xml.

Page 14: 13 caching   latest

© JBoss, Inc. 2003, 2004. 14

Professional Open Source™

You can configure EHCache through the configuration file ehcache.xml, located in the source root folder.

Page 15: 13 caching   latest

© JBoss, Inc. 2003, 2004. 15

Professional Open Source™

To monitor Hibernate’s caching activities at runtime, add the following line to the log4j configuration file log4j.properties:

Page 16: 13 caching   latest

© JBoss, Inc. 2003, 2004. 16

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.

Page 17: 13 caching   latest

© JBoss, Inc. 2003, 2004. 17

Professional Open Source™

Configuring cache usage strategy

Page 18: 13 caching   latest

© JBoss, Inc. 2003, 2004. 18

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:

Page 19: 13 caching   latest

© JBoss, Inc. 2003, 2004. 19

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.

Page 20: 13 caching   latest

© JBoss, Inc. 2003, 2004. 20

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:

Page 21: 13 caching   latest

© JBoss, Inc. 2003, 2004. 21

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);

Page 22: 13 caching   latest

© JBoss, Inc. 2003, 2004. 22

Professional Open Source™

CacheMode options are provided to control the interaction of Hibernate with the second level cache:

Page 23: 13 caching   latest

© JBoss, Inc. 2003, 2004. 23

Professional Open Source™

CacheModes

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.

Page 24: 13 caching   latest

© JBoss, Inc. 2003, 2004. 24

Professional Open Source™

CACHING ASSOCIATIONS

Page 25: 13 caching   latest

© JBoss, Inc. 2003, 2004. 25

Professional Open Source™

Is a book’s associated publisher object cached when its parent book object is cached?

Page 26: 13 caching   latest

© JBoss, Inc. 2003, 2004. 26

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

Page 27: 13 caching   latest

© JBoss, Inc. 2003, 2004. 27

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>

Page 28: 13 caching   latest

© JBoss, Inc. 2003, 2004. 28

Professional Open Source™

Caching Collections

factory.evictCollection("com.training.Book.chapters");factory.evictCollection("com.training.Book.chapters", id);

Page 29: 13 caching   latest

© JBoss, Inc. 2003, 2004. 29

Professional Open Source™

Caching Queries

By default, the HQL queries aren’t cached. You must first enable the query cache in the Hibernate configuration file:

Page 30: 13 caching   latest

© JBoss, Inc. 2003, 2004. 30

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.