VOOZH about

URL: https://deepwiki.com/hypervel/devtool/6.1-cache-system-table-migrations

⇱ Cache System Table Migrations | hypervel/devtool | DeepWiki


Loading...
Menu

Cache System Table Migrations

Purpose and Scope

This page documents the cache system table migration generators: CacheTableCommand and CacheLocksTableCommand. These commands generate database migrations for the database cache driver, creating tables to store cached key-value pairs and atomic cache locks.

Related documentation: Queue System Table Migrations #6.3 Session Table Migration #6.2 Database Migration Generators #6


Overview

The cache system provides two migration generators for database-backed caching:

CommandClassTable PurposeDefault NameConfig Key
make:cache-tableCacheTableCommandCache key-value storagecachecache.stores.database.table
make:cache-locks-tableCacheLocksTableCommandAtomic lock managementcache_lockscache.stores.database.lock_table

Both classes extend Hyperf\Devtool\Generator\GeneratorCommand and implement identical patterns: retrieve table name from ConfigInterface, generate timestamped migration filename, process stub template with %TABLE% replacement, write to database/migrations/.

Sources: src/Generator/CacheTableCommand.php15-106 src/Generator/CacheLocksTableCommand.php15-106


Command Architecture

CacheTableCommand Execution Flow


Sources: src/Generator/CacheTableCommand.php29-59 src/Generator/CacheTableCommand.php100-105 src/Generator/CacheTableCommand.php61-64 src/Generator/CacheTableCommand.php66-69


CacheTableCommand

The CacheTableCommand class generates a migration for the primary cache storage table.

Command Registration

The CacheTableCommand::__construct() registers the command name as make:cache-table, and configure() sets the description:

Command: make:cache-table
Description: Create a migration for the cache database table
Parent: Hyperf\Devtool\Generator\GeneratorCommand

Sources: src/Generator/CacheTableCommand.php17-27

Execution Implementation

The execute(InputInterface $input, OutputInterface $output): int method at src/Generator/CacheTableCommand.php29-59 implements these steps:

  1. Call migrationTableName() to retrieve table name from ConfigInterface::get('cache.stores.database.table', 'cache')
  2. Generate filename: Carbon::now()->format('Y_m_d_000000') . "_create_{$tableName}_table.php"
  3. Determine path: $input->getOption('path') ?: "database/migrations/{$filename}"
  4. Check alreadyExists($path) and $input->getOption('force'), abort if file exists without force flag
  5. Call makeDirectory($path) to ensure parent directories exist
  6. Load stub via file_get_contents($this->getStub())
  7. Process stub with buildMigration($stub, $tableName) which performs str_replace('%TABLE%', $name, $stub)
  8. Write migration with file_put_contents($path, $processedStub)
  9. Output success message and call openWithIde($path)
  10. Return 0

Sources: src/Generator/CacheTableCommand.php29-59 src/Generator/CacheTableCommand.php100-105 src/Generator/CacheTableCommand.php61-64

Configuration Integration

The migrationTableName() method at src/Generator/CacheTableCommand.php100-105 retrieves the table name:


The method uses ApplicationContext::getContainer() to resolve ConfigInterface, then reads the configuration key cache.stores.database.table with fallback default 'cache'.

Sources: src/Generator/CacheTableCommand.php100-105

Command Options

The getOptions() method at src/Generator/CacheTableCommand.php81-90 defines:

OptionShortTypeDescription
--path-pInputOption::VALUE_OPTIONALCustom migration path (overrides database/migrations/{filename})
--force-fBoolean (inherited)Overwrite existing migration without check

The method filters parent options to remove the default path option, then adds a custom path option specific to migration table commands.

Sources: src/Generator/CacheTableCommand.php81-90

Stub Template Resolution

The getStub() method at src/Generator/CacheTableCommand.php66-69 returns the stub file path:


Default stub: src/Generator/stubs/cache-table.stub. The configuration array $this->getConfig()['stub'] allows overriding the stub path for custom table schemas.

Sources: src/Generator/CacheTableCommand.php66-69


CacheLocksTableCommand

The CacheLocksTableCommand class generates a migration for atomic cache lock management.

Command Registration

The CacheLocksTableCommand::__construct() registers the command as make:cache-locks-table:

Command: make:cache-locks-table
Description: Create a migration for the cache locks database table
Parent: Hyperf\Devtool\Generator\GeneratorCommand

Sources: src/Generator/CacheLocksTableCommand.php17-27

Implementation Differences from CacheTableCommand

CacheLocksTableCommand implements the same execute() flow as CacheTableCommand with two differences:

  1. migrationTableName() at src/Generator/CacheLocksTableCommand.php100-105 reads cache.stores.database.lock_table (default: 'cache_locks')
  2. getStub() at src/Generator/CacheLocksTableCommand.php66-69 returns stubs/cache-locks-table.stub

All other methods (execute(), buildMigration(), alreadyExists(), getOptions(), getDefaultNamespace()) are identical implementations.

Sources: src/Generator/CacheLocksTableCommand.php100-105 src/Generator/CacheLocksTableCommand.php66-69 src/Generator/CacheLocksTableCommand.php29-95

Table Schema Purpose

Cache locks enable atomic operations on cached data, preventing race conditions when multiple processes attempt to access or modify the same cache key simultaneously.


Generated Migration Structure

Cache Table Schema

The cache-table.stub at src/Generator/stubs/cache-table.stub15-19 defines this schema:



































ColumnMethod CallTypeConstraintsPurpose
key$table->string('key')->primary()VARCHARPrimary keyCache identifier
value$table->mediumText('value')MEDIUMTEXTNoneSerialized cached value
expiration$table->integer('expiration')INTEGERNoneTTL unix timestamp

Sources: src/Generator/stubs/cache-table.stub15-19

Cache Locks Table Schema

The cache-locks-table.stub at src/Generator/stubs/cache-locks-table.stub15-19 defines:



































ColumnMethod CallTypeConstraintsPurpose
key$table->string('key')->primary()VARCHARPrimary keyLock identifier
owner$table->string('owner')VARCHARNoneProcess/owner identifier
expiration$table->integer('expiration')INTEGERNoneLock expiration timestamp

Sources: src/Generator/stubs/cache-locks-table.stub15-19

Migration Template Structure

Both stub templates follow this structure:


The up() method calls Schema::create('%TABLE%', function (Blueprint $table) { ... }) to define columns. The down() method calls Schema::dropIfExists('%TABLE%'). The %TABLE% placeholder is replaced by buildMigration().

Sources: src/Generator/stubs/cache-table.stub1-29 src/Generator/stubs/cache-locks-table.stub1-29


Stub Processing Mechanism

Placeholder Replacement

The buildMigration(string $stub, string $name): string method performs stub processing:


This method replaces all occurrences of %TABLE% in the stub with the table name. Both CacheTableCommand and CacheLocksTableCommand implement identical versions.

Sources: src/Generator/CacheTableCommand.php61-64 src/Generator/CacheLocksTableCommand.php61-64

Processing Pipeline Sequence


Sources: src/Generator/CacheTableCommand.php29-59 src/Generator/CacheTableCommand.php100-105 src/Generator/CacheTableCommand.php61-64


File System Operations

Migration File Naming Convention

Both commands generate filenames at src/Generator/CacheTableCommand.php35 and src/Generator/CacheLocksTableCommand.php35:


Example: 2024_01_15_000000_create_cache_table.php

The format Y_m_d_000000 uses zero time component, allowing multiple migrations created on the same day to be ordered by their specific table name suffix.

Sources: src/Generator/CacheTableCommand.php35 src/Generator/CacheLocksTableCommand.php35

Output Path Determination

Path resolution at src/Generator/CacheTableCommand.php36:


Default: database/migrations/{filename}. The --path option overrides this default. Paths are relative to BASE_PATH.

Sources: src/Generator/CacheTableCommand.php36 src/Generator/CacheLocksTableCommand.php36

Directory Creation

The makeDirectory($path) call at src/Generator/CacheTableCommand.php49 ensures parent directories exist. This method is inherited from GeneratorCommand and creates directories recursively.

Sources: src/Generator/CacheTableCommand.php49 src/Generator/CacheLocksTableCommand.php49

Existence Checking

The alreadyExists(string $rawName): bool method at src/Generator/CacheTableCommand.php71-74:


Returns true if the file exists at BASE_PATH/{$rawName}. The execute() method checks this result combined with $input->getOption('force') to prevent overwrites.

Sources: src/Generator/CacheTableCommand.php71-74 src/Generator/CacheLocksTableCommand.php71-74 src/Generator/CacheTableCommand.php41-44


Usage Examples

Basic Usage

Generate cache table migration:


Output at src/Generator/CacheTableCommand.php54:

Migration 2024_01_15_000000_create_cache_table.php created successfully.

Generate cache locks table migration:


Output at src/Generator/CacheLocksTableCommand.php54:

Migration 2024_01_15_000000_create_cache_locks_table.php created successfully.

Sources: src/Generator/CacheTableCommand.php54 src/Generator/CacheLocksTableCommand.php54

Custom Path

Specify custom migration location:


Force Overwrite

Regenerate existing migration file:


Configuration-Based Table Names

When config/autoload/cache.php defines custom table names:


The migrationTableName() method reads these values via ConfigInterface::get(), generating migrations with app_cache and app_cache_locks table names.

Sources: src/Generator/CacheTableCommand.php100-105 src/Generator/CacheLocksTableCommand.php100-105


Integration with Hypervel Framework

Dependency on Configuration System


Both commands import Hyperf\Context\ApplicationContext and Hyperf\Contract\ConfigInterface at src/Generator/CacheTableCommand.php8-9 The migrationTableName() method resolves ConfigInterface from the container to read table names.

Sources: src/Generator/CacheTableCommand.php8-9 src/Generator/CacheTableCommand.php100-105 src/Generator/CacheLocksTableCommand.php8-9 src/Generator/CacheLocksTableCommand.php100-105

Migration Class Inheritance

Generated migrations extend Hypervel\Database\Migrations\Migration:


The stub imports this at src/Generator/stubs/cache-table.stub6 and creates an anonymous class at line 9 that extends it. This provides access to Hypervel's migration runner features.

Sources: src/Generator/stubs/cache-table.stub6 src/Generator/stubs/cache-table.stub9 src/Generator/stubs/cache-locks-table.stub6 src/Generator/stubs/cache-locks-table.stub9

Schema Facade

The stub templates use Hypervel\Support\Facades\Schema for fluent table definitions:


This provides a consistent API across all Hypervel migrations.

Sources: src/Generator/stubs/cache-table.stub7-15

Refresh this wiki

On this page