FileCache
class FileCache implements FileCache
The file cache.
Constants
| LOCK_DIR |
Name of the subdirectory in the cache path that holds the per-process
lock files. |
| REFS_DIR |
Name of the subdirectory in the cache path that holds the reference links. |
| MAX_RETRIEVE_ATTEMPTS |
Maximum number of times retrieve() retries when the cached file keeps
disappearing or is left empty by a failing writer. Bounds the retries so
a persistently failing file cannot loop forever. |
Properties
| protected int | $linkCount | Counter for reference links used to generate unique names per instance. |
|
| protected array | $config | File cache configuration. |
|
| protected Filesystem | $files | The Filesytem instance to use |
|
| protected FilesystemManager | $storage | File FilesystemManager instance to use |
|
| protected ClientInterface | $client | Guzzle HTTP client to use |
|
| protected resource|null | $lockHandle | Open file handle holding the exclusive lock that signals this process is alive. Created lazily and held for the lifetime of the process. |
|
| protected string|null | $lockToken | Unique token identifying this process. Embedded in every reference link name and used as the name of this process' lock file. |
Methods
Create an instance.
Release the process lock when the instance is destroyed. If this does not run (e.g. on a crash) the kernel releases the lock anyway and the orphan lock file is reaped by the pruner.
Perform a callback with the paths of many cached files. Use this to prevent pruning of the files while they are processed.
Like batch but deletes the cached files afterwards (if they are not used
somewhere else).
Retrieve the files, run the callback with their paths and clean up the reference links afterwards.
Remove cached files that are too old or exceed the maximum cache size.
Delete all unused cached files.
Throw if the given MIME type is not in the configured allow list.
Throw if the given file size exceeds the configured max_file_size. A negative max_file_size disables the check.
Get path and reference link for a file that exists in the cache.
Get path and reference link for a file that does not yet exist in the cache.
Create the reference link that signals the cached file is in use and release the lock handle.
Get a Finder for the canonical cache files, excluding reference links and lock files.
Extract the owning process' lock token from a reference link filename.
Acquire the lock that signals this process is alive, holding it for the lifetime of the process.
Determine whether the process owning the given lock token is still alive.
Remove reference links left behind by crashed workers.
Prune canonical files that are older than max_age.
Prune oldest canonical files that exceed the max_size.
Remove lock files of processes that have terminated.
Get the directory in which process lock files are stored.
Get the directory in which reference links are stored.
Get the path to the lock file for the given process token.
Cache an file from a storage disk and get the path to the cached file. Files from local disks are not cached.
Store the file from the given resource to a cached file.
Creates the directory if it doesn't exist yet.
Get the stream resource for an file.
Create a new Guzzle HTTP client.
Details
at line 105
__construct(array $config = [], Filesystem $files = null, FilesystemManager $storage = null, $client = null)
Create an instance.
at line 118
__destruct()
Release the process lock when the instance is destroyed. If this does not run (e.g. on a crash) the kernel releases the lock anyway and the orphan lock file is reaped by the pruner.
at line 132
bool
exists(File $file)
Check if a file exists.
at line 144
mixed
get(File $file, callable $callback, bool $throwOnLock = false)
Perform a callback with the path of a cached file. This takes care of shared locks on the cached file file so it is not corrupted due to concurrent write operations.
at line 154
mixed
getOnce(File $file, callable $callback, bool $throwOnLock = false)
Like get but deletes the cached file afterwards (if it is not used somewhere
else).
at line 164
resource
getStream(File $file)
Get a stream resource for an file. If the file is cached, the resource points to the cached file instead. This will not cache uncached files. Make sure to close the streams!
at line 200
mixed
batch(array $files, callable $callback, bool $throwOnLock = false)
Perform a callback with the paths of many cached files. Use this to prevent pruning of the files while they are processed.
at line 213
mixed
batchOnce(array $files, callable $callback, bool $throwOnLock = false)
Like batch but deletes the cached files afterwards (if they are not used
somewhere else).
at line 233
protected mixed
runBatch(array $files, callable $callback, bool $throwOnLock, callable $cleanup)
Retrieve the files, run the callback with their paths and clean up the reference links afterwards.
at line 267
prune()
Remove cached files that are too old or exceed the maximum cache size.
at line 286
clear()
Delete all unused cached files.
at line 311
protected bool
existsRemote(File $file)
Check for existence of a remte file.
at line 357
protected bool
existsDisk(File $file)
Check for existence of a file from a storage disk.
at line 386
protected
assertMimeTypeAllowed(string $type)
Throw if the given MIME type is not in the configured allow list.
at line 401
protected
assertSizeAllowed(int $size)
Throw if the given file size exceeds the configured max_file_size. A negative max_file_size disables the check.
at line 417
protected bool
delete(SplFileInfo $file)
Delete a cached file it it is not used.
at line 472
protected array
retrieve(File $file, bool $throwOnLock = false, int $attempt = 0)
Cache a remote or cloud storage file if it is not cached and get the path to the cached file. If the file is local, nothing will be done and the path to the local file will be returned.
at line 567
protected array
retrieveExistingFile(string $cachedPath, resource $handle)
Get path and reference link for a file that exists in the cache.
at line 588
protected array
retrieveNewFile(File $file, string $cachedPath, resource $handle)
Get path and reference link for a file that does not yet exist in the cache.
at line 648
protected string
createReferenceLink(string $cachedPath, resource $handle)
Create the reference link that signals the cached file is in use and release the lock handle.
at line 681
protected Finder
canonicalFiles()
Get a Finder for the canonical cache files, excluding reference links and lock files.
at line 698
protected string
referenceLinkToken(string $filename)
Extract the owning process' lock token from a reference link filename.
at line 713
protected
ensureProcessLock()
Acquire the lock that signals this process is alive, holding it for the lifetime of the process.
The lock is created lazily the first time a reference link is needed. The kernel releases it when the process exits (including crashes), which is how the pruner detects that this worker's reference links are stale. flock also works across (Docker) containers sharing the cache volume because the lock lives in the host kernel on the shared inode.
at line 745
protected bool
isProcessAlive(string $token)
Determine whether the process owning the given lock token is still alive.
at line 775
protected
pruneStaleReferences()
Remove reference links left behind by crashed workers.
at line 810
protected int
pruneFilesByMaxAge()
Prune canonical files that are older than max_age.
at line 844
protected
pruneFilesByMaxSize(int $currentSize)
Prune oldest canonical files that exceed the max_size.
at line 894
protected
pruneOrphanLockFiles()
Remove lock files of processes that have terminated.
at line 936
protected string
lockDir()
Get the directory in which process lock files are stored.
at line 946
protected string
refsDir()
Get the directory in which reference links are stored.
at line 958
protected string
lockPath(string $token)
Get the path to the lock file for the given process token.
at line 973
protected string
getDiskFile(File $file, resource $target)
Cache an file from a storage disk and get the path to the cached file. Files from local disks are not cached.
at line 1016
protected string
cacheFromResource(File $file, resource $source, resource $target)
Store the file from the given resource to a cached file.
at line 1054
protected
ensureDirExists(string $dir)
Creates the directory if it doesn't exist yet.
at line 1068
protected string
getCachedPath(File $file)
Get the path to the cached file file.
at line 1082
protected resource
getFileStream(string $url)
Get the stream resource for an file.
at line 1098
protected bool
isRemote(File $file)
Determine if an file is remote, i.e. served by a public webserver.
at line 1110
protected Filesystem
getDisk(File $file)
Get the storage disk on which a file is stored.
at line 1123
protected ClientInterface
makeHttpClient()
Create a new Guzzle HTTP client.