PHP code example of eribloo / laravel-cache-objects

1. Go to this page and download the library: Download eribloo/laravel-cache-objects 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/ */

    

eribloo / laravel-cache-objects example snippets


/**
 * @implements CacheObject<CarbonInterface>
 *
 * @method static self make(User $user)
 */
final readonly class SomeUserCache implements CacheObject
{
    /** @use CacheObjectActions<CarbonInterface> */
    use CacheObjectActions;

    public function __construct(
        public User $user,
    ) {}

    public function key(): StringKey
    {
        $id = $this->user->getKey();

        return new StringKey("some-cache:$id");
    }

    public function ttl(): CarbonInterval
    {
        return CarbonInterval::minutes(15);
    }

    /**
     * @return SerializeTransformer<CarbonInterface>
     */
    public function transformer(): SerializeTransformer
    {
        return new SerializeTransformer();
    }
}


$user = User::findOrFail(1);

$key  = SomeUserCache::make($user)->store(now());  // 'some-cache:1'
$date = SomeUserCache::make($user)->retrieve();    // App\Support\Carbon object
$bool = SomeUserCache::make($user)->delete();      // true

return [
    'namespace' => 'App\Cache',
];

public function key(): EriBloo\CacheObjects\Contracts\Key;
public function ttl(): Carbon\CarbonInterval;
public function transformer(): EriBloo\CacheObjects\Contracts\Transformer;

public function key(): EriBloo\CacheObjects\ValueObjects\Keys\StringKey
{
    return new StringKey(key: 'some-cache');
}

public function key(): EriBloo\CacheObjects\ValueObjects\Keys\HashedKey
{
    return new HashedKey(
        key: new StringKey('some-cache'), 
        algo: 'md5',
    );
}

public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\JsonTransformer
{
    return new JsonTransformer(
        loadFlags: JSON_INVALID_UTF8_SUBSTITUTE,
        saveFlags: JSON_UNESCAPED_UNICODE,
        depth: 256,
    );
}

public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\SerializeTransformer
{
    return new SerializeTransformer(allowedClasses: [SomeClass::class]);
}

public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\EncryptedTransformer
{
    return new EncryptedTransformer(
        transformer: new SerializeTransformer,
    );
}

public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\GuardTransformer
{
    return new GuardTransformer(
        transformer: new EncryptedTransformer(new SerializeTransformer),
        onSaveGuard: function (CarbonInterface $value) {
            if ($value->isPast()) {
                throw new UnexpectedValueException;
            }
        },
        onLoadGuard: null,
    );
}

public static function make(): static; // easier creation
public function store(mixed $value): string; // put into storage, returns key stored in cache
public function retrieve(): mixed; // get from storage
public function delete(): bool; // remove from storage
protected function resolveDriver(): EriBloo\CacheObjects\Contracts\Driver; // resolves to default driver from Service Provider, more below

final class CacheObjectStored
{
    public function __construct(
        public CacheObject $cacheObject,
        public mixed $originalValue,
        public string $transformedValue,
    ) {}
}

final class CacheObjectRetrieved
{
    public function __construct(
        public CacheObject $cacheObject,
        public string $originalValue,
        public mixed $transformedValue,
    ) {}
}

final class CacheObjectMissed
{
    public function __construct(
        public CacheObject $cacheObject,
    ) {}
}

final class CacheObjectDeleted
{
    public function __construct(
        public CacheObject $cacheObject,
    ) {}
}