PHP code example of ulex / cached-repositories

1. Go to this page and download the library: Download ulex/cached-repositories 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/ */

    

ulex / cached-repositories example snippets


php artisan vendor:publish --provider="Ulex\CachedRepositories\RepositoriesServiceProvider" --tag=config

'providers' => [
// ...
Ulex\CachedRepositories\RepositoriesServiceProvider::class,
];

$app->register(Ulex\CachedRepositories\RepositoriesServiceProvider::class);
 
$app->configure('cached-repositories');

php artisan make:repository Post --all

Repository created successfully.
Interface created successfully.
Decorator created successfully.
Add Model in `models` array in config/cached-repositories.php

Decorators
Eloquent
Interfaces

...
'models' => [
        'User' => App\Models\User::class,
        'Post' => App\Models\Post::class,
]
...

# Example when injecting to a controller 

public function __construct(UserRepositoryInterface $userRepository)
{
    $this->userRepository = $userRepository;
}

...

public function get($name)
{
    //retrieve from db and then cache the result
    $user = $this->userRepository->getBy('name', $userName);
    //retrieve straight from db, don't cache
    $user = $this->userRepository->fromDb()->getBy('name', $userName);
} 

public function getUserInfo($user_id)
{
    return $this->remember(__FUNCTION__, func_get_args());
}

public function flushGetKeys($model, $attributes = null)
{
    $user_id = $model->user_id;
    $key = $this->key('getUserInfo', compact('user_id'));
    parent::flushGetKeys($model, $attributes);
}

public function getUserInfo($user_id)
{
    return $this->model->query()
        ->where('user_id', '=', $user_id)
        ->whereNotNull('something')->get();
}

cp vendor/ulex/cached-repositories/config/cached-repositories.php config/cached-repositories.php