PHP code example of stevenmaguire / laravel-cache

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

    

stevenmaguire / laravel-cache example snippets


class UserRegistrar extends Stevenmaguire\Laravel\Services\EloquentCache
{
    //
}

class UserRegistrar implements Stevenmaguire\Laravel\Contracts\Cacheable
{
    use \Stevenmaguire\Laravel\Services\EloquentCacheTrait;
}

use App\User;
use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function getAllUsers()
    {
        $query = $this->user->query();

        return $this->cache('all', $query);
    }

    public function getUserById($id)
    {
        $query = $this->user->where('id', $id);

        return $this->cache('id('.$id.')', $query, 'first');
    }

    public function getRecent($skip = 0, $take = 100)
    {
        $query = $this->user->orderBy('created_at', 'desc')
            ->take($take)
            ->skip($skip);

        return $this->cache('recent('.$skip.','.$take.')', $query);
    }
}

/**
 * Paginate users with all pagination parameters
 */
public function getAllUsers()
{
    $query = $this->user->query();

    return $this->cache('all', $query, 'paginate:15,id|email|name,sheet,2');
    // $query->paginate(15, ['id', 'email', 'name'], 'sheet', 2);
}

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $cacheForMinutes = 15;

    //
}

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableCaching = false;

    //
}

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableLogging = false;

    //
}

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $cacheIndexKey = 'my-service-keys-index';

    //
}

$userRegistrar->flushCache();

// Flush cache for all cached users with a single digit user id
$userRegistrar->flushCache('^id\([0-9]{1}\)$');

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->when('App\Handlers\Events\UserHandler')
          ->needs('Stevenmaguire\Laravel\Contracts\Cacheable')
          ->give('App\Services\UserRegistrar');
    }
}