PHP code example of ginnerpeace / illuminate-cache-database

1. Go to this page and download the library: Download ginnerpeace/illuminate-cache-database 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/ */

    

ginnerpeace / illuminate-cache-database example snippets



return [
    // ....
    'providers' => [
        // ...
        Zeigo\Illuminate\CacheDatabase\RedisHashProvider::class,
    ],
    // Its optional.
    'aliases' => [
        // ...
        'RedisHashQuery' => Zeigo\Illuminate\CacheDatabase\Facades\RedisHashQuery::class,
    ],
    // ...
];

$app->register(Zeigo\Illuminate\CacheDatabase\LumenRedisHashProvider::class);



namespace DataRepository;

use App\Models\User;
use Zeigo\Illuminate\CacheDatabase\Contracts\RedisHashRepository;

class Users implements RedisHashRepository
{
    public function version(): string
    {
        return '1.0';
    }

    /** TTL (seconds). */
    public function ttl(): int
    {
        return 60;
    }

    public function fetch(array $ids, string $scope = null): array
    {
        // The $scope param is design for data sharding.
        // Use or not is up to u.
        // User::{$scope}()->find($ids)
        $result = User::whereType($scope)->find($ids, [
            'id',
            'username',
        ]);

        if ($result->isEmpty()) {
            return [];
        }

        return $result->keyBy('id')->toArray();
    }
}

return [
    'connection' => 'cache',
    'prefix' => 'hash-database',
    'repositories' => [
        'users' => DataRepository\Users::class,
    ],
];

// Data from redis hash table: "hash-database:users"
RedisHashQuery::table('users')->get([1, 2, 3]);
// dump
[
    1 => [
        'id' => 1,
        'username' => 'First user',
    ],
    2 => [
        'id' => 2,
        'username' => 'Second user',
    ],
    // no data
    3 => null,
];

// Data from redis hash table: "hash-database:users:scopeName"
RedisHashQuery::from('scopeName', 'users')->find(9);
[
    'id' => 9,
    'username' => '999',
];
bash
php artisan vendor:publish --provider="Zeigo\Illuminate\CacheDatabase\RedisHashProvider"