-
Notifications
You must be signed in to change notification settings - Fork 0
Home
DCache is a a property driven caching framework that allows you to configure multi-storage-medium backed caching framework for Spring. Allowing you to simply define your preferred cache ID, backing mechanism, key and value types and just autowiring in the cache and using it.
When using this as a dependency you may need to ensure that you scan the following package au.kilemonn.dcache.config
to ensure the @Component
classes are initialised correctly. This can be done with @ComponentScan(basePackages = ["au.kilemonn.dcache.config"])
on your main application class.
If your classes are still unable to wire in the instances you may need to make them dependent on the configuration class by using @DependsOn(DCacheConfiguration.DCACHE_CACHE_MANAGER)
.
Please refer to DCache-Example for an example of setup and include the configuration classes.
The general property syntax for defining the cache instances uses the following format, dcache.cache.\<cache-id\>.\<property\>
.
Each unique cache-id
that is provided will be detected and configured as a new cache as long as its other properties satisfy the configuration requirements.
The available properties are as follows:
This property is required
This sets the backing cache type that should be used for the defined cache type. Available values are:
E.g. dcache.cache.my-cache.type=IN_MEMORY
This property is required
This property is used to set the expected key type for the specific cache instance. This should be a fully qualified class name. E.g. java.lang.String
.
NOTE: Using MEMCACHED, the key_class is locked to java.lang.String
.
E.g. dcache.cache.my-cache.key_class=java.lang.String
This property is required
This property is used to set the expected value type for the specific cache instance. This should be a fully qualified class name. E.g. java.lang.String
.
E.g. dcache.cache.my-cache.value_class=java.lang.String
This property is optional
This property is used to set a constant prefix which will be pre-pended to all provided keys before accessing the cache. This is only used if the key_class type is a String type. E.g. Using prefix "my-prefix-" and key "my-key", then the key will be evaluated to "my-prefix-my-key" before accessing or setting values into the cache.
E.g. dcache.cache.my-cache.prefix=my-prefix-
This property is optional
This sets one of the existing caches as the current cache's fallback. This is only applicable if this cache type is configured to be REDIS or MEMCACHED since the IN_MEMORY cache would never become unavailable.
In the case where the current cache is unavailable, all cache interactions are forwarded to the configured fallback cache. The expected value is the cache-id
of another defined cache in the properties configuration.
E.g. dcache.cache.my-cache.fallback=fallback-cache
This property is required when REDIS or MEMCACHED is configured as the type
This sets the remote endpoint of the REDIS or MEMCACHED instance to connect to, this should include the port as well.
E.g. dcache.cache.my-redis-cache.endpoint=redis:6379
This property is optional and only used when REDIS or MEMCACHED is configured as the type
Sets the timeout in milliseconds for cache interactions. After this timeout the cache result failure will be handled and if a fallback has been configured it will be interacted with instead of the current cache instance. By default this is 0.
E.g. dcache.cache.my-redis-cache.timeout=200
As well as wiring and creating the DCache
instances, a single instance of the DCacheManager will be created.
Lets say we have the following configuration:
dcache.cache.my-redis.type=REDIS
dcache.cache.my-redis.key_class=au.kilemonn.MyKey
dcache.cache.my-redis.value_class=au.kilemonn.MyObject
dcache.cache.my-redis.endpoint=redis:6379
dcache.cache.my-redis.timeout=200
dcache.cache.my-redis.prefix=my-key-
dcache.cache.my-redis.fallback=in-memory-fallback
dcache.cache.in-memory-fallback.type=IN_MEMORY
dcache.cache.in-memory-fallback.key_class=au.kilemonn.MyKey
dcache.cache.in-memory-fallback.value_class=au.kilemonn.MyObject
Then we can autowire both caches (or in this case just the redis cache since the in-memory cache is configured as a fallback).
The defined cache-id
is required as a @Qualifier
when wiring in the DCache
object.
// @DependsOn(DCacheConfiguration.DCACHE_CACHE_MANAGER) - this may be required
public class AClass {
@Autowired
@Qualifier("my-redis")
private DCache<MyKey, MyObject> redisCache;
@Autowired
@Qualifier("in-memory-fallback")
private DCache<MyKey, MyObject> inMemoryCache;
...
}