Here is a list of some of the most common methods you can use to interact with CacheBox. For full details, please view the online API Docs
addCache(any<ICacheProvider> cache)
Register a new instantiated cache with this cache factory
addDefaultCache(string name)
Add a default named cache to our registry, create it, config it, register it and return it of type: cachebox
clearAll()
Clears all the elements in all the registered caches without de-registrations
configure(CacheBoxConfig config)
Configure the cache factory for operation, called by the init()
expireAll()
Expires all the elements in all the registered caches without de-registrations
getCache(string name)
Get a reference to a registered cache in this factory
getDefaultCache()
Get the default cache provider of type cachebox
reapAll()
A nice way to call reap on all registered caches
removeCache(string name)
Try to remove a named cache from this factory
replaceCache(any<ICacheProvider> cache, any<ICacheProvider> decoratedCache)
Replace a registered named cache with a new decorated cache of the same name
shutdown()
Recursively sends shutdown commands to all registered caches and cleans up in preparation for shutdown
Info Remember that some of the CacheBox methods announce events. So please see our event model section to see what kind of events you can listen to when working with CacheBox.
CacheBox is the core framework you need to instantiate in order to work with caching in your application. You can instantiate it with a CacheBoxConfig
object that will hold all of your caching configurations. To configure the library you can either do it programmatically or via a struct/data DSL, the choice is yours. After the library is instantiated and configured you can ask from it a named cache provider, or interact with the caches. By default, CacheBox can be started with no configuration and it will configure itself with the default configuration that is shipped with CacheBox that can be found in the following location:
The default configuration is the following:
If you are using CacheBox within a ColdBox application you do not need to worry about creating or even configuring the framework, as ColdBox ships already with a default configuration for ColdBox applications. However, you also have full control of configuring CacheBox at your application level (Please see the ColdBox application configuration section). The default configuration for a ColdBox application can be found in the following location:
In summary, CacheBox has two modes of operation:
Standalone Framework
ColdBox Application
If you have downloaded CacheBox as a standalone framework, then please make sure you use the correct namespace path (cachebox.system
). Also, once you create CacheBox make sure that you persist it somewhere, either in Application scope or any other scope or a dependency injection context (WireBox). ColdBox application users already have an instance of CacheBox created for you in every application and it is stored in the main application controller and can be retrieved via the following function:
Info Note: Most of the examples shown in this documentation refer the default framework namespace of coldbox.system. However, if you are using CacheBox as a standalone framework, then the namespace to use is
cachebox.system
. Happy Coding!
Let's start with the simplest example of them all which is just creating CacheBox with the default configuration. The rest of the samples show different configuration options:
Here are some more configuration samples:
If you are using CacheBox within a ColdBox application, ColdBox is in charge of emitting reaping or cleanup requests to the CacheBox factory. However, if you are using the standalone version of the cache, then you will need to emit this command yourself in the desired execution point. We would suggest to do this on every request. This does not mean that the request executes every time, it means that the trigger is made and if the reaping time has been elapsed, then the caches clean themselves (CacheBox Providers Only!):
That's it! Just call on the reapAll() method and this will make sure that your caches clean themselves according to the reaping frequency selected in your configuration file.
Info: Reaping frequency and cleanup only applies to the CacheBox Provider.