////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

All of the plugin's classes are designed for extensibility; the classes are all public, and fields and methods are mostly public or protected. Consider subclassing existing classes to reuse as much as possible instead of completely rewriting them.

== Cache manager


The core cache plugin registers a `grailsCacheManager` Spring bean, and the extension plugins replace this bean with one that creates and manages caches for that implementation. The default implementation is an instance of link:{api}grails/plugin/cache/GrailsConcurrentMapCacheManager.html[GrailsConcurrentMapCacheManager] which uses link:{api}grails/plugin/cache/GrailsConcurrentMapCache.html[GrailsConcurrentMapCache] as its cache implementation. It uses a `java.util.concurrent.ConcurrentHashMap` to store cached values.

You can customize the cache manager by replacing the `grailsCacheManager` Spring bean in `resources.groovy` with your own; either subclass link:{api}grails/plugin/cache/GrailsConcurrentMapCacheManager.html[GrailsConcurrentMapCacheManager] (e.g. to override the `createConcurrentMapCache()` method) or by implementing the link:{api}org/grails/plugin/cache/GrailsCacheManager.html[GrailsCacheManager] interface.

=== Fragment caching


You can cache partial GSP page sections with the `<cache:block>` tag. You can specify a key when using this tag but it's in general unnecessary. This is because the block will be rendered with its own Closure, and the default key is the full closure class name. This is unique since the closures aren't re-used; for example these two blocks will be cached independently, even in the same GSP:

[source,groovy]
----
<cache:block>
foo
</cache:block>

<cache:block>
bar
</cache:block>
----

You can cache the content of templates with the `<cache:render>` tag. You can specify a key when using this tag but like the `block` tag, it's in general unnecessary because the default key is the full template class name.


=== Service caching

You can cache the return value of a service method by annotating it with link:{api}grails/plugin/cache/Cacheable.html[Cacheable].

==== Key generation

The default key generated implements the link:{api}grails/plugin/cache/GrailsCacheKeyGenerator.html[GrailsCacheKeyGenerator] which provides method for generating keys taking into account:

* The class name
* The method name
* A hashCode for the current object
* The parameters or if a closure is specified to generate the key the value of the closure call

You can override this implementation by defining a bean called `customCacheKeyGenerator` via Spring.

