= Title Cache

:Notice: 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. http://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.

The _Titlecache_ extension module provides a simple way to cache titles of entities for a configurable duration.
This can speed up the rendering time of data-heavy objects.

The current implementation uses the link:https://github.com/ben-manes/caffeine[Caffeine] caching library.

== Dependency Management

[source,xml,subs="attributes+"]
.pom.xml
----
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.causeway.extensions</groupId>
            <artifactId>causeway-extensions-titlecache</artifactId>
            <scope>import</scope>
            <type>pom</type>
            <version>{page-causewayreleaseversion}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
----

[#dependencies]
=== Dependencies

For the module of every entity whose title you want to cache, add the following dependency:

[source,xml,subs="attributes+"]
.pom.xml
----
<dependencies>
    <dependency>
        <groupId>org.apache.causeway.extensions</groupId>
        <artifactId>causeway-extensions-titlecache-applib</artifactId>
    </dependency>
</dependencies>
----

And, in the webapp module of your application, add the following dependency:

[source,xml,subs="attributes+"]
.pom.xml
----
<dependencies>
    <dependency>
        <groupId>org.apache.causeway.extensions</groupId>
        <artifactId>causeway-extensions-titlecache-caffeine</artifactId>
    </dependency>
</dependencies>
----

[#appmanifest]
=== AppManifest

In your application's `AppManifest` (top-level Spring `@Configuration` used to bootstrap the app), import the `CausewayModuleExtTitlecacheCaffeine` module.

[source,java]
.AppManifest.java
----
@Configuration
@Import({
        ...
        CausewayModuleExtTitlecacheCaffeine.class,
        ...
})
public class AppManifest {
}
----


== Usage

For each entity class whose title is to be cached, create a subclass of xref:refguide:applib:index/events/ui/TitleUiEvent.adoc[] that also implements the xref:refguide:extensions:index/titlecache/applib/event/Cached.adoc[Cached] marker interface, and mark it as the event class fpr xref:refguide:applib:index/annotation/DomainObjectLayout.adoc#titleUiEvent[@DomainObjectLayout#titleUiEvent]:

[source,java]
----
@DomainObject
@DomainObjectLayout(
    titleUiEvent = Customer.TitleUiEvent.class
)
public class Customer /*...*/ {

    public static class TitleUiEvent extends CausewayModuleApplib.TitleUiEvent<Customer> implements Cached { }

    // ...
}
----

The titles of these entities will be cached according to the default xref:#configuration[configuration].

Or, for more control on an given entity, implement the xref:refguide:extensions:index/titlecache/applib/event/CachedWithCacheSettings.adoc[] interface:

[source,java]
----
@DomainObject
@DomainObjectLayout(
    titleUiEvent = Customer.TitleUiEvent.class
)
public class Customer /*...*/ {

    public static class TitleUiEvent extends CausewayModuleApplib.TitleUiEvent<Customer> implements CachedWithCacheSettings {
        @Override public int expiryDurationInMinutes() { return 1;}
        @Override public int maxSizeInEntries() {return 50;}
    }

    // ...
}
----



[#configuration]
== Configuration

Two configuration properties can be set:

* xref:refguide:config:sections/causeway.extensions.adoc#causeway.extensions.titlecache.caffeine.expiry-duration-in-minutes[causeway.extensions.titlecache.caffeine.expiry-duration-in-minutes]
* xref:refguide:config:sections/causeway.extensions.adoc#causeway.extensions.titlecache.caffeine.max-size-in-entries[causeway.extensions.titlecache.caffeine.max-size-in-entries]

