PHP code example of highperapp / cache

1. Go to this page and download the library: Download highperapp/cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

highperapp / cache example snippets



use HighPerApp\Cache\ServiceProvider;

// Auto-configure from environment
$cache = ServiceProvider::createFromEnv();

// Or configure manually
$cache = ServiceProvider::createWithConfig([
    'engine' => 'auto',
    'default_store' => 'redis',
    'redis' => [
        'host' => '127.0.0.1',
        'port' => 6379
    ]
]);

// Store data
yield $cache->set('user:123', ['name' => 'John', 'email' => '[email protected]'], 3600);

// Retrieve data
$user = yield $cache->get('user:123');

// Check if exists
$exists = yield $cache->has('user:123');

// Delete data
yield $cache->delete('user:123');

$config = [
    'default_store' => 'redis',
    'redis' => [
        'host' => '127.0.0.1',
        'port' => 6379,
        'password' => null,
        'database' => 0,
        'pool_min' => 5,
        'pool_max' => 20
    ]
];

$config = [
    'default_store' => 'valkey',
    'valkey' => [
        'host' => '127.0.0.1',
        'port' => 6379,
        'memory_efficiency' => true,
        'multi_threading' => true
    ]
];

$config = [
    'default_store' => 'dragonfly',
    'dragonfly' => [
        'host' => '127.0.0.1',
        'port' => 6379,
        'snapshot_enabled' => true,
        'memory_defrag' => true
    ]
];

$config = [
    'default_store' => 'aws_elasticache',
    'aws_elasticache' => [
        'cluster_endpoint' => 'my-cluster.cache.amazonaws.com',
        'port' => 6379,
        'tls' => true,
        'multi_az' => true,
        'auto_failover' => true
    ]
];

$config = [
    'redis' => [
        'cluster' => [
            'enabled' => true,
            'type' => 'cluster',
            'nodes' => [
                ['host' => '127.0.0.1', 'port' => 7000],
                ['host' => '127.0.0.1', 'port' => 7001],
                ['host' => '127.0.0.1', 'port' => 7002]
            ],
            'read_preference' => 'secondary',
            'auto_discovery' => true
        ]
    ]
];

use HighPerApp\Cache\Session\SessionStore;

$provider = new ServiceProvider();
$provider->register();

// Get session store
$sessionStore = $provider->get('session.store');

// Use as PHP session handler
$sessionHandler = $provider->get('session.handler');
session_set_save_handler($sessionHandler, true);
session_start();

// Direct session operations
$sessionStore->write('sess_123', 'session_data');
$data = $sessionStore->read('sess_123');
$sessionStore->destroy('sess_123');

use HighPerApp\Cache\Middleware\CacheMiddleware;

$middleware = new CacheMiddleware($cache, $config);

// Configure cache patterns
$config = [
    'cache' => [
        's
$stats = $middleware->getStats();

// Auto-select best available engine
$cache = ServiceProvider::createFromEnv();

// Force specific engine
$cache = ServiceProvider::createWithConfig([
    'engine' => 'rust_ffi'  // or 'amphp', 'pure_php'
]);

$stats = yield $cache->getStats();
/*
Array
(
    [hits] => 1250
    [misses] => 89
    [hit_ratio] => 0.933
    [memory_usage] => 45231616
    [keys_count] => 342
    [engine] => 'rust_ffi'
    [provider] => 'redis'
)
*/

// Get multiple keys
$data = yield $cache->getMultiple(['user:1', 'user:2', 'user:3']);

// Set multiple keys
yield $cache->setMultiple([
    'user:1' => $userData1,
    'user:2' => $userData2
], 3600);

// Delete multiple keys
yield $cache->deleteMultiple(['user:1', 'user:2']);

// Store with tags
yield $cache->setWithTags('product:123', $product, ['products', 'catalog'], 3600);

// Invalidate by tag
yield $cache->invalidateByTag('products');