VOOZH about

URL: https://deepwiki.com/hypervel/cache/1.1-installation-and-configuration

⇱ Installation and Configuration | hypervel/cache | DeepWiki


Loading...
Menu

Installation and Configuration

This page provides instructions for installing the hypervel/cache package and configuring its various cache store backends. It covers Composer installation, framework integration, configuration file structure, and the settings available for each supported cache driver (array, file, redis, swoole, stack, database).

For information about the system architecture and how these components work together, see Architecture Overview. For details on framework-specific integration mechanisms, see Framework Integration.


Installation

Composer Package Installation

The package is installed via Composer with the following command:


Sources: composer.json1-53

System Requirements

The package requires the following dependencies:

DependencyVersionPurpose
PHP^8.2Minimum PHP version
hyperf/config~3.1.0Configuration management
hyperf/support~3.1.0Framework utilities
laravel/serializable-closure^1.3Closure serialization
psr/simple-cache^3.0PSR-16 cache interface

Sources: composer.json31-36

Optional Dependencies

Additional packages are required for specific cache drivers:

PackageVersionRequired For
hyperf/redis~3.1.0Redis cache driver
hypervel/cache~0.1.0File-based locking

Sources: composer.json38-41


Configuration File Publishing

The package integrates with Hyperf through the ConfigProvider class, which registers services, event listeners, console commands, and publishable configuration files.

Publishing the Configuration

To publish the default configuration file, use the Hyperf vendor publish command:


This copies the configuration from the package to your application:

  • Source: vendor/hypervel/cache/publish/cache.php
  • Destination: config/autoload/cache.php

Sources: src/ConfigProvider.php31-38

Configuration Provider Registration

The ConfigProvider class automatically registers when the package is installed:


Diagram: ConfigProvider Registration Flow

The ConfigProvider registers the following components:

  • Dependencies: Binds Factory::class to CacheManager::class and Store::class to the default driver
  • Listeners: CreateSwooleTable initializes Swoole tables on server start, CreateTimer sets up interval tasks
  • Commands: ClearCommand and PruneDbExpiredCommand for cache management
  • Publishable Config: The cache.php configuration file

Sources: src/ConfigProvider.php1-41 composer.json45-48


Configuration File Structure

The published configuration file defines the cache system's behavior. It contains four main sections:


Diagram: Configuration File Structure

Sources: publish/cache.php1-111

Default Cache Store

The default key specifies which store to use when no explicit store is requested:

Configuration KeyEnvironment VariableDefault ValueDescription
defaultCACHE_DRIVER'array'The default cache store name

Sources: publish/cache.php22

Cache Stores Section

The stores array defines all available cache backends. Each store has a driver key that determines its implementation.

Supported drivers: array, file, redis, swoole, stack, database, null

Sources: publish/cache.php37-83

Swoole Tables Configuration

The swoole_tables section defines Swoole shared memory tables used by the swoole driver:

Configuration KeyDefault ValueDescription
swoole_tables.default.rows1024Maximum number of rows
swoole_tables.default.bytes10240Memory size per row in bytes
swoole_tables.default.conflict_proportion0.2Hash collision buffer ratio

Sources: publish/cache.php91-96

Cache Key Prefix

The prefix setting prevents key collisions when multiple applications share the same cache backend:

Configuration KeyEnvironment VariableDefault BehaviorDescription
prefixCACHE_PREFIXAPP_NAME slug + _cachePrefix for all cache keys

The default prefix is generated from the APP_NAME environment variable, converted to a slug format.

Sources: publish/cache.php110


Store Configuration

Each cache store driver has specific configuration options. The following sections detail the configuration for each supported driver.


Diagram: Store Driver Configuration Mapping

Sources: publish/cache.php37-83

Array Store Configuration

The array driver stores cache entries in memory for the current process only. It is primarily used for testing and development.

Configuration KeyTypeDefaultDescription
driverstring'array'Driver identifier
serializeboolfalseWhether to serialize values

Configuration Example:


Sources: publish/cache.php38-41

File Store Configuration

The file driver stores cache entries as files on the filesystem.

Configuration KeyTypeDefaultDescription
driverstring'file'Driver identifier
pathstringBASE_PATH . '/storage/cache/data'Directory for cache files
lock_pathstringBASE_PATH . '/storage/cache/data'Directory for lock files

Configuration Example:


Sources: publish/cache.php43-47

Redis Store Configuration

The redis driver uses Redis for distributed caching.

Configuration KeyTypeDefaultDescription
driverstring'redis'Driver identifier
connectionstring'default'Redis connection name from config/autoload/redis.php
lock_connectionstring'default'Redis connection for distributed locks

Configuration Example:


The connection value references a connection defined in the Hyperf Redis configuration file.

Sources: publish/cache.php49-53

Swoole Store Configuration

The swoole driver uses Swoole shared memory tables for high-performance caching across workers.

Configuration KeyTypeDefaultDescription
driverstring'swoole'Driver identifier
tablestring'default'Swoole table name from swoole_tables config
memory_limit_bufferfloat0.05Memory buffer ratio (5%) before eviction
eviction_policyintSwooleStore::EVICTION_POLICY_LRUEviction algorithm constant
eviction_proportionfloat0.05Proportion of entries to evict (5%)
eviction_intervalint10000Cleanup interval in milliseconds

Eviction Policy Constants:

  • SwooleStore::EVICTION_POLICY_LRU - Least Recently Used
  • SwooleStore::EVICTION_POLICY_LFU - Least Frequently Used
  • SwooleStore::EVICTION_POLICY_TTL - Time To Live based
  • SwooleStore::EVICTION_POLICY_NONE - No automatic eviction

Configuration Example:


For detailed information on eviction policies, see Memory Management and Eviction.

Sources: publish/cache.php55-62

Stack Store Configuration

The stack driver combines multiple cache stores into a multi-tier caching strategy.

Configuration KeyTypeDefaultDescription
driverstring'stack'Driver identifier
storesarraySee exampleArray of store configurations with optional TTL overrides

Store Array Structure:

Each element in the stores array can be either:

  • A string store name: 'redis'
  • An associative array with TTL override: 'swoole' => ['ttl' => 3]

Configuration Example:


This configuration creates a two-tier cache where swoole acts as a fast L1 cache with a 3-second TTL, and redis serves as the L2 persistent cache. For more information, see Stack Store.

Sources: publish/cache.php64-72

Database Store Configuration

The database driver stores cache entries in a SQL database table.

Configuration KeyTypeEnvironment VariableDefaultDescription
driverstring-'database'Driver identifier
connectionstringDB_CACHE_CONNECTIONDB_CONNECTION or 'default'Database connection name
tablestringDB_CACHE_TABLE'cache'Cache entries table name
lock_connectionstring|nullDB_CACHE_LOCK_CONNECTIONnullDatabase connection for locks (null uses connection)
lock_tablestringDB_CACHE_LOCK_TABLE'cache_locks'Lock entries table name
lock_lotteryarray-[2, 100]Lottery odds for lock cleanup [chances, total]
lock_timeoutint-86400Lock expiration timeout in seconds

Configuration Example:


Lock Lottery: The [2, 100] configuration means there is a 2 in 100 chance that expired locks will be cleaned up on each lock acquisition attempt.

Sources: publish/cache.php74-82


Environment Variables

The configuration file supports the following environment variables for runtime configuration:

Environment VariableDefaultPurposeUsed In
CACHE_DRIVER'array'Default cache storeDefault store selection
CACHE_PREFIXGenerated from APP_NAMECache key prefixKey prefixing
APP_NAME'hyperf'Application namePrefix generation
DB_CACHE_CONNECTIONFalls back to DB_CONNECTIONDatabase connectionDatabase store
DB_CONNECTION'default'Default DB connectionDatabase store fallback
DB_CACHE_TABLE'cache'Cache table nameDatabase store
DB_CACHE_LOCK_CONNECTIONnullLock DB connectionDatabase locks
DB_CACHE_LOCK_TABLE'cache_locks'Lock table nameDatabase locks

Sources: publish/cache.php22 publish/cache.php76-79 publish/cache.php110


Console Commands

The package provides console commands for cache management, which are automatically registered by the ConfigProvider:

CommandClassPurpose
cache:clearClearCommand::classClear all cache entries
cache:prune-db-expiredPruneDbExpiredCommand::classRemove expired entries from database cache

For detailed usage information, see Console Commands.

Sources: src/ConfigProvider.php27-30 src/Console/PruneDbExpiredCommand.php1-66


Event Listeners

The package registers event listeners that initialize resources during server startup:


Diagram: Swoole Table Initialization Flow

ListenerEventPurpose
CreateSwooleTable::classBeforeServerStart::classInitializes Swoole tables before server starts
CreateTimer::classBeforeServerStart::classSets up interval timers for cleanup tasks

The CreateSwooleTable listener iterates through all configured stores with driver: 'swoole' and initializes their corresponding Swoole tables via the SwooleTableManager.

Sources: src/ConfigProvider.php23-26 src/Listeners/CreateSwooleTable.php1-25