Description | manuals and libraries |
Kernel::System::Cache - Key/value based data cache for OTRS
This is a simple data cache. It can store key/value data both in memory and in a configured cache backend for persistent caching.
This can be controlled via the config settings Cache::InMemory
and Cache::InBackend
. The backend can also be selected with the config setting Cache::Module
and defaults to file system based storage for permanent caching.
create an object. Do not use it directly, instead use:
use Kernel::System::ObjectManager;
local $Kernel::OM = Kernel::System::ObjectManager->new();
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
change cache configuration settings at runtime. You can use this to disable the cache in environments where it is not desired, such as in long running scripts.
please, to turn CacheInMemory off in persistent environments.
$CacheObject->Configure(
CacheInMemory => 1, # optional
CacheInBackend => 1, # optional
);
store a value in the cache.
$CacheObject->Set(
Type => 'ObjectName', # only [a-zA-Z0-9_] chars usable
Key => 'SomeKey',
Value => 'Some Value',
TTL => 60 * 60 * 24 * 20, # seconds, this means 20 days
);
The Type here refers to the group of entries that should be cached and cleaned up together, usually this will represent the OTRS object that is supposed to be cached, like 'Ticket'.
The Key identifies the entry (together with the type) for retrieval and deletion of this value.
The TTL
controls when the cache will expire. Please note that the in-memory cache is not persistent and thus has no TTL
/expiry mechanism.
Please note that if you store complex data, you have to make sure that the data is not modified in other parts of the code as the in-memory cache only refers to it. Otherwise also the cache would contain the modifications. If you cannot avoid this, you can disable the in-memory cache for this value:
$CacheObject->Set(
Type => 'ObjectName',
Key => 'SomeKey',
Value => { ... complex data ... },
TTL => 60 * 60 * 24 * 1, # optional, default 20 days
CacheInMemory => 0, # optional, defaults to 1
CacheInBackend => 1, # optional, defaults to 1
);
fetch a value from the cache.
my $Value = $CacheObject->Get(
Type => 'ObjectName', # only [a-zA-Z0-9_] chars usable
Key => 'SomeKey',
);
Please note that if you store complex data, you have to make sure that the data is not modified in other parts of the code as the in-memory cache only refers to it. Otherwise also the cache would contain the modifications. If you cannot avoid this, you can disable the in-memory cache for this value:
my $Value = $CacheObject->Get(
Type => 'ObjectName',
Key => 'SomeKey',
CacheInMemory => 0, # optional, defaults to 1
CacheInBackend => 1, # optional, defaults to 1
);
deletes a single value from the cache.
$CacheObject->Delete(
Type => 'ObjectName', # only [a-zA-Z0-9_] chars usable
Key => 'SomeKey',
);
Please note that despite the cache configuration, Delete and CleanUp will always be executed both in memory and in the backend to avoid inconsistent cache states.
delete parts of the cache or the full cache data.
To delete the whole cache:
$CacheObject->CleanUp();
To delete the data of only one cache type:
$CacheObject->CleanUp(
Type => 'ObjectName', # only [a-zA-Z0-9_] chars usable
);
To delete all data except of some types:
$CacheObject->CleanUp(
KeepTypes => ['Object1', 'Object2'],
);
To delete only expired cache data:
$CacheObject->CleanUp(
Expired => 1, # optional, defaults to 0
);
Type/KeepTypes and Expired can be combined to only delete expired data of a single type or of all types except the types to keep.
Please note that despite the cache configuration, Delete and CleanUp will always be executed both in memory and in the backend to avoid inconsistent cache states.
This software is part of the OTRS project (https://otrs.org/).
This software comes with ABSOLUTELY NO WARRANTY. For details, see the enclosed file COPYING for license information (GPL). If you did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.