2017 © Pedro Peláez
 

library laravel-cache

Seamlessly adding caching to Laravel service objects

image

stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  • Friday, October 23, 2015
  • by stevenmaguire
  • Repository
  • 3 Watchers
  • 19 Stars
  • 1,702 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 1 % Grown

The README.md

Laravel Cache Services

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads, (*1)

Seamlessly adding caching to Laravel Eloquent service objects., (*2)

Install

Via Composer, (*3)

``` bash $ composer require stevenmaguire/laravel-cache, (*4)


## Usage ### Include the base service Extend your service class with the EloquentCache class. ```php class UserRegistrar extends Stevenmaguire\Laravel\Services\EloquentCache { // }

Implement the interface and use the trait., (*5)

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

Construct queries

Build queries using Eloquent and request cache object., (*6)

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);
    }
}

The cache method takes three parameters:, (*7)

  • The unique key associated with the method's intentions
  • The query Builder object for the Eloquent query
  • The optional verb, get, first, list, paginate etc; get by default

If the method associated with the optional verb takes parameters, like paginate, the parameters can be expressed as a comma separated list following the verb and a colon. If a parameter expects an array of literal values, these may be expressed as a pipe delimited sting., (*8)

/**
 * 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);
}

The cache service will automatically index all of the unique keys used by your application. These keys will be used when the flushCache method is called on each service implementing the base cache service., (*9)

Configure caching

For each of the services you implement using the EloquentCache you can configure the following:, (*10)

Duration of cache minutes

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $cacheForMinutes = 15;

    //
}

Disable caching

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableCaching = false;

    //
}

Disable logging

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableLogging = false;

    //
}

Set a custom cache index for the keys

use Stevenmaguire\Laravel\Services\EloquentCache;

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

    //
}

Flushing cache

Each service object that implements caching via Stevenmaguire\Laravel\Services\EloquentCache can flush its own cache, independently of other consuming services., (*11)

Your application can flush the cache for all keys within the service object serviceKey group., (*12)

$userRegistrar->flushCache();

Your application can only flush the cache for keys within the service object serviceKey group that match a particular regular expression pattern., (*13)

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

Bind to IoC Container

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');
    }
}

In this particular example, UserHandler is responsible for flushing the user service cache when a specific event occurs. The UserHandler takes a dependacy on the flushCache method within the UserRegistrar service., (*14)

Testing

bash $ ./vendor/bin/phpunit, (*15)

Contributing

Please see CONTRIBUTING for details., (*16)

Security

If you discover any security related issues, please email stevenmaguire@gmail.com instead of using the issue tracker., (*17)

Credits

License

The MIT License (MIT). Please see License File for more information., (*18)

The Versions

23/10 2015

dev-master

9999999-dev https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

23/10 2015

1.0.0

1.0.0.0 https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

22/10 2015

0.1.6

0.1.6.0 https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

22/10 2015

dev-improve-key-management

dev-improve-key-management https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

20/10 2015

0.1.4

0.1.4.0 https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

20/10 2015

dev-add-flushCache-regex

dev-add-flushCache-regex https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

20/10 2015

0.1.3

0.1.3.0 https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

20/10 2015

dev-add-verb-parameters

dev-add-verb-parameters https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

11/09 2015

0.1.2

0.1.2.0 https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

14/05 2015

0.1.1

0.1.1.0 https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire

28/04 2015

0.1.0

0.1.0.0 https://github.com/stevenmaguire/laravel-cache

Seamlessly adding caching to Laravel service objects

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm laravel cache eloquent query builder stevenmaguire