NAME
Storage::Abstract::Driver::Composite - Use multiple sources of storage
SYNOPSIS
my $storage = Storage::Abstract->new(
driver => 'composite',
sources => [
{
driver => 'directory',
directory => '/some/dir',
readonly => !!1,
}
{
driver => 'memory'
},
],
);
DESCRIPTION
This driver can hold a number of drivers under itself (in sequence) and choose the first driver which holds a given file.
Choosing the source
This driver will use the following logic to find a source suitable to store / retrieve a file:
Check the "sources" array in order, starting from index 0.
If the source is readonly, skip it if the operation being performed is modifying the storage.
If the source doesn't report having this file (as with
is_stored
), skip it (unless we are storing).If the source encounters an exception, write it into "errors" and skip it.
If the source was not skipped in the previous steps, use it.
After the first successful pairing of a path with a source, it will be cached. Future operations on this path will prefer to use the cached source, but if they were to fail, they will fall back to checking all sources once again.
Unless you want to possibly have duplicated files in your sources (due to the driver falling back to other sources on exceptions), you should mark all but one nested drivers as readonly.
CUSTOM INTERFACE
Attributes
sources
Required - An array reference of Storage::Abstract instances. Each instance be coerced from a hash reference, which will be used to call "new" in Storage::Abstract. Their order is significant - they will be tried in sequence.
errors
This is an array reference which will be populated with source errors if they occur. Each element in the array will be an array reference of two elements - the first element will be the source instance from "sources", while the second one will be the exception which was caught.
This structure can be examined to see whether any of the sources encountered errors when performing their operations. It's probably wise to examine it when catching Storage::Abstract::X::StorageError
.
It cannot be set in the constructor, obviously.
Methods
clear_cache
This method will clear the internal cache of the driver.