Download the PHP package iviphp/cache without Composer
On this page you can find all versions of the php package iviphp/cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package cache
Short Description Cache stores and cache management for the IviPHP ecosystem.
License MIT
Informations about the package cache
Ivi Cache
Cache stores and cache management for the IviPHP ecosystem.
Overview
iviphp/cache provides a small, framework-independent cache layer for PHP applications.
It includes:
- a common cache contract;
- in-memory caching;
- persistent filesystem caching;
- named cache stores;
- lazy store resolution;
- configurable default stores;
- cache expiration;
- permanent cache entries;
- atomic file replacement;
- per-key filesystem locks;
- cache integrity checks;
- optional payload compression;
- safe cache exceptions;
- no global static cache state.
The package currently provides two cache drivers:
arrayfile
Installation
Requirements
- PHP 8.2 or later
- Composer
iviphp/support
The filesystem store can optionally use the PHP Zlib extension for payload compression.
Package structure
Basic usage
Cache configuration
The recommended configuration structure contains a default store and a map of named stores:
The configured default store must exist.
Direct store configuration
A direct map of named stores is also accepted:
In this example, the store named default becomes the default cache store.
Array store
The array store keeps values in the current PHP process.
The array store is useful for:
- automated tests;
- request-scoped caching;
- command execution;
- temporary runtime values;
- applications that do not require persistent cache data.
Values disappear when the PHP process ends.
Objects are stored directly and retain their normal PHP reference behavior.
File store
The file store persists values on the filesystem.
The configured directory is created automatically by default.
The directory must be readable and writable by the PHP process.
File store configuration
Supported file-store options:
| Option | Default | Description |
|---|---|---|
directory |
required | Filesystem cache directory |
path |
none | Alias of directory |
allowed_classes |
false |
Classes allowed during serialization |
compression_threshold |
1024 |
Minimum serialized size before compression |
compression_level |
6 |
Zlib compression level from 0 to 9 |
maximum_entry_bytes |
67108864 |
Maximum cache entry size |
file_permissions |
0600 |
Permissions applied to cache files |
directory_permissions |
0700 |
Permissions applied to cache directories |
create_directory |
true |
Create the cache directory when missing |
Storing values
Store a value for a limited time:
The integer lifetime is expressed in seconds.
Permanent values
Store a value without expiration:
The following call is equivalent:
Retrieving values
Provide a default value:
The default value is returned when the key:
- does not exist;
- has expired;
- was deleted.
Cached null values
Cached null values are treated as existing entries.
Use has() when the difference between a missing entry and a cached null value matters.
Checking existence
Expired entries are treated as missing.
Deleting values
Deleting a missing key is considered successful.
Retrieving and deleting
Use pull() to retrieve and remove a value:
After the operation, the key no longer exists.
Clearing a store
Clear the selected store:
For the file store, the root cache directory is preserved.
The global store-lock file is also preserved.
Clearing every store
Every registered store is resolved and cleared.
Remembering values
Retrieve an existing value or calculate and cache it:
The resolver runs only when the key does not exist or has expired.
Remembering permanent values
This value remains cached until it is explicitly deleted or the store is cleared.
DateInterval expiration
A DateInterval can be used instead of an integer:
Remember a value for one hour:
Immediate expiration
A lifetime of zero or fewer seconds removes the existing entry:
The value is not stored.
Multiple cache stores
Use the default store:
Use another store:
The original cache instance remains unchanged.
Return to the manager default:
Cache manager
Create a manager directly:
Create a cache entry point:
Resolving stores
Resolve the default store:
Resolve a named store:
get() is an alias of store():
Stores are resolved lazily and reused after their first creation.
Registering a store
Register a configured store:
Register an existing store instance:
Store factories
A closure can create a custom store lazily:
The factory must return an implementation of CacheInterface.
Replacing a store
The previously resolved store instance is discarded.
The new definition is resolved lazily.
Removing a store
When the removed store was the default, the first remaining registered store becomes the new default.
Store information
Check whether a store is registered:
Check whether it has already been resolved:
Return all registered store names:
Return the default store name:
Change the default store:
The store must already be registered.
Clearing resolved stores
Clear only stores that were already resolved:
Stores that have not yet been resolved are not created.
Clearing all registered stores
Every registered store is resolved and cleared.
Removing all definitions
This removes all registered definitions and resolved store instances.
It does not automatically clear persistent cache data before removing the definitions.
Using stores directly
ArrayStore
FileStore
Array-store inspection
The array store provides additional inspection methods.
Count unexpired entries:
Return all unexpired keys:
Remove expired entries:
File-store maintenance
The file store also provides maintenance methods.
Return the canonical cache directory:
Remove expired entries:
Count valid entries:
File storage format
File cache entries use:
- SHA-256 key hashing;
- two-level directory sharding;
- JSON metadata envelopes;
- Base64-encoded serialized payloads;
- SHA-256 integrity checks;
- optional Zlib compression;
- atomic temporary-file replacement;
- per-key lock files;
- a global store lock.
Original cache keys are not used as filenames.
A key such as:
is converted to a SHA-256 hash before creating its storage path.
Atomic writes
The file store writes data to a temporary file before replacing the destination cache file.
This reduces the risk of partially written cache entries.
Per-key locks protect concurrent write operations for the same cache key.
Cache stampede protection
FileStore::remember() holds a per-key lock while resolving a missing value.
Concurrent processes requesting the same missing key wait for the first resolver instead of calculating the value simultaneously.
This protection applies to the filesystem store.
The array store is scoped to one PHP process and does not provide inter-process locking.
Compression
Serialized payloads larger than the configured threshold may be compressed:
Compression is only retained when the compressed payload is smaller than the original serialized payload.
When Zlib is unavailable, values are stored without compression.
Maximum entry size
The default maximum cache entry size is 64 MiB:
The limit protects the application from unexpectedly large cache files and decompressed payloads.
Caching objects
The file store disables object restoration by default:
This is the safest default when cache files could be modified outside the application.
Allow specific classes:
Allow every serialized class:
Allowing every class should only be used when the cache directory is fully trusted and protected.
Unsupported file-cache values
The file store rejects values that cannot be safely serialized, including:
- resources;
- closures;
- objects that are not included in
allowed_classes; - values exceeding the configured size limit.
The array store can keep closures and most runtime values because it does not serialize them.
Cache keys
Cache keys must:
- be non-empty;
- contain no invalid control characters;
- contain no null bytes;
- be no longer than 512 bytes.
Valid examples:
Keys are application-defined and may contain common separators.
Cache exceptions
Cache failures throw:
Example:
Safe exception context
CacheException provides diagnostic context without including cached values or serialized payloads.
Possible context fields include:
The original cache key is not included in serialization, read, write and deletion failure contexts.
Exception factories
Cache interface
Custom cache stores must implement:
The contract defines:
Custom cache store
Register the custom store:
Complete example
Security considerations
Protect the cache directory
The file cache directory must not be writable by untrusted users.
Recommended permissions:
The cache directory should not be publicly accessible through the web server.
Do not cache secrets unnecessarily
Avoid caching sensitive values unless required.
Examples include:
- passwords;
- access tokens;
- payment credentials;
- private encryption keys;
- authentication secrets;
- personal information.
Serialized data
PHP serialized data must only be restored from trusted cache files.
The default file-store configuration disables object restoration:
Prefer arrays and scalar values for persistent cache entries.
Cache is not permanent storage
Cache data may be:
- deleted;
- expired;
- corrupted;
- unavailable;
- cleared during deployment.
Do not use cache storage as the only source of important application data.
Cache keys are not authorization
The presence of a cache entry does not prove that a user is authorized to access the underlying data.
Authorization checks must remain in the application.
Design principles
- Small framework-independent API
- Explicit named stores
- Lazy store resolution
- No global static cache state
- Cached null-value support
- Clear expiration semantics
- Safe default object deserialization
- Atomic filesystem writes
- Per-key filesystem locks
- Cache stampede reduction
- Hashed filesystem paths
- Integrity-checked cache envelopes
- Optional transparent compression
- Protected cache directory permissions
- Safe diagnostic context
- Extensible cache-store contract
Package boundaries
This package is responsible for:
- cache contracts;
- cache value storage;
- expiration handling;
- named store management;
- array caching;
- filesystem caching;
- file-lock coordination;
- cache exceptions.
It is not responsible for:
- database queries;
- HTTP sessions;
- authentication;
- authorization;
- queue processing;
- distributed locking;
- Redis protocols;
- Memcached protocols;
- application configuration loading;
- framework bootstrapping.
Additional cache drivers can be provided by separate IviPHP packages.
Current limitations
The initial release does not include:
- Redis;
- Memcached;
- database-backed caching;
- distributed cache locks;
- cache tags;
- cache namespaces;
- increment and decrement operations;
- PSR-6 adapters;
- PSR-16 adapters;
- asynchronous cache operations.
These features can be added later without expanding the core package unnecessarily.
Ecosystem
This package is part of the IviPHP ecosystem:
iviphp/contractsiviphp/supportiviphp/configiviphp/containeriviphp/httpiviphp/validationiviphp/databaseiviphp/cacheiviphp/viewiviphp/authiviphp/framework
Contributing
Contributions should preserve the focused and secure design of the package.
Changes should:
- preserve cached null-value behavior;
- preserve cache-key validation;
- avoid exposing cached values in exceptions;
- preserve atomic filesystem writes;
- preserve file-lock coordination;
- maintain safe object-deserialization defaults;
- remain independent from the full framework;
- avoid introducing hidden global state;
- include clear failures for invalid configuration;
- work on supported PHP 8.2 environments.
Security
Please report security vulnerabilities privately to the Softadastra maintainers instead of opening a public issue.
License
This project is licensed under the MIT License.
Maintainer
Created and maintained by Softadastra.