public class CacheKey extends Object
SelfLoadingCache
facade and the related CacheProvider.
Each key is intended to identify a specific type of objects,
therefore to be created a CacheKey requires:
The data model version can be very useful when working with an external cache server. If you have multiple application servers that access the same cache server and you need to release a new version of the application that changes the cached data model, during the release the application servers with the old code should still retrieve the old models from the cache while the application servers with the new code should populate the cache with the new models. This is automatically achieved by using versions.
You may want to cache two different objects of the same type, with the same attributes.
For example you want to cache two sets of UserStats for the same User,
so the data model type will be UserStats and the attribute will be the user ID
but you need to cache two different values in the same cache. Also in this case
you can use the version to distinguish the two cache keys. You can create two different
cache keys like:
CacheKey dailyCacheKey = CacheKey.of( UserStats.class, "daily", 1 );
CacheKey monthlyCacheKey = CacheKey.of( UserStats.class, "monthly", 1 );
To make it easier to build keys from data this class provides
a CacheKey.Prototype that can be defined static and configured
with type and version. The CacheKey.Prototype is thread-safe
and can be defined as a static singleton object.
The common use case for this prototype object is the following:
public class SomeManager
{
The static prototype to use where needed.
private static final CacheKey.Prototype myDataModelCacheKey = CacheKey.prototype( MyDataModel.class, "1.0.0" );
...
public void someMethod()
{
CacheKey actualCacheKey = myDataModelCacheKey.of( list of attributes );
...
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
CacheKey.Prototype
Prototype class populated with the data model type and version.
|
| Modifier and Type | Field and Description |
|---|---|
protected static String |
DEFAULT_VERSION
Value to use if the user do not provide a custom version.
|
| Modifier | Constructor and Description |
|---|---|
protected |
CacheKey()
Default constructor.
|
protected |
CacheKey(Class<?> dataModelType,
String version,
Object[] attributes)
Constructor with parameters.
|
| Modifier and Type | Method and Description |
|---|---|
Stream<Object> |
attributes()
Returns the attributes defining this cache key.
|
Class<?> |
dataModelType()
Returns the type of the data model identified by this cache key.
|
boolean |
equals(Object other) |
int |
hashCode() |
static CacheKey |
of(Class<?> dataModelType,
Object... attributes)
Creates a new cache key with the given values.
|
static CacheKey |
of(Class<?> dataModelType,
String version,
Object... attributes)
Creates a new cache key with the given values.
|
static CacheKey.Prototype |
prototype(Class<?> dataModelType)
Creates a new cache key prototype with the given data model type.
|
static CacheKey.Prototype |
prototype(Class<?> dataModelType,
String version)
Creates a new cache key prototype with the given values.
|
String |
toString() |
String |
version()
Returns the version of the data model identified by this cache key.
|
protected static final String DEFAULT_VERSION
protected CacheKey()
This constructor is intended to be used by reflection during de-serialization.
To create a new cache key use the factory methods.
protected CacheKey(Class<?> dataModelType, String version, Object[] attributes)
This constructor is intended to be used by extending classes only.
To create a new cache key use the factory methods.
dataModelType - the type of the data model pointed by this cache key.version - the version of the data model pointed by this cache key.attributes - the key attributes used to define the unique identifier.public static CacheKey of(Class<?> dataModelType, Object... attributes)
dataModelType - the type of the data model pointed by this cache key.attributes - the key attributes used to define the unique identifier.public static CacheKey of(Class<?> dataModelType, String version, Object... attributes)
dataModelType - the type of the data model pointed by this cache key.version - the version of the data model pointed by this cache key.attributes - the key attributes used to define the unique identifier.public static CacheKey.Prototype prototype(Class<?> dataModelType)
dataModelType - the type of the data model pointed by this cache key.public static CacheKey.Prototype prototype(Class<?> dataModelType, String version)
dataModelType - the type of the data model pointed by this cache key.version - the version of the data model pointed by this cache key.public Class<?> dataModelType()
public String version()
public Stream<Object> attributes()
Copyright © 2011–2020 Nerd4j. All rights reserved.