Skip to content

Instantly share code, notes, and snippets.

@0xMatt
Last active August 29, 2015 14:13
Show Gist options
  • Save 0xMatt/465a936b5916210d4d84 to your computer and use it in GitHub Desktop.
Save 0xMatt/465a936b5916210d4d84 to your computer and use it in GitHub Desktop.
Cache Decoration
<?php
namespace Lithe\Database;
use Illuminate\Cache\CacheManager;
use Lithe\Contracts\Database\CacheInterface;
class Cache implements CacheInterface
{
/**
*
* @var Illuminate\Cache\CacheManager
*/
protected $cache;
/**
*
* @var string
*/
protected $tag;
/**
*
* @var integer
*/
protected $minutes;
/**
*
* @param CacheManager $cache
* @param unknown $tag
* @param number $minutes
*/
public function __construct(CacheManager $cache, $tag, $minutes = 60)
{
$this->cache = $cache;
$this->tag = $tag;
$this->minutes = $minutes;
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\CacheInterface::get()
*/
public function get($key)
{
return $this->cache->tags($this->tag)->get($key);
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\CacheInterface::put()
*/
public function put($key, $value, $minutes = null)
{
if (is_null($minutes)) {
$minutes = $this->minutes;
}
return $this->cache->tags($this->tag)->put($key, $value, $minutes);
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\CacheInterface::has()
*/
public function has($key)
{
return $this->cache->tags($this->tag)->has($key);
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\CacheInterface::forever()
*/
public function remember($key, callable $callback)
{
return $this->cache->tags($this->tag)->remember($key, $this->minutes, $callback());
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\CacheInterface::remember()
*/
public function forever($key, callable $callback)
{
return $this->cache->tags($this->tag)->forever($key, $callback());
}
/**
*
* @param unknown $key
*/
public function delete($key)
{
return $this->cache->tags($this->tag)->forget($key);
}
}
<?php
namespace Lithe\Database;
use Lithe\Contracts\Database\RepositoryInterface;
abstract class CacheDecorator extends Repository implements RepositoryInterface
{
/**
* The repository object
*
* @var object
*/
protected $repo;
/**
*
* @var unknown
*/
protected $cache;
/**
*
* @param Container $app
*/
public function __construct(RepositoryInterface $repo, $cache)
{
parent::__construct();
$this->repo = $repo;
$this->cache = $cache;
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Database\CacheDecorator::all()
*/
public function all(array $columns = ['*'])
{
return $this->cache->forever('all', function () use($columns) {
return $this->repo->all($columns);
});
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Database\CacheDecorator::find()
*/
public function find($id, array $columns = ['*'])
{
return $this->cache->forever("id.{$id}", function () use($id, $columns) {
return $this->repo->find($id, $columns);
});
}
/*
* (non-PHPdoc)
* @see \Lithe\Database\Repository::create()
*/
public function create(array $attributes)
{
$user = $this->repo->create($attributes);
$this->cache->put(md5('id.' . $user->id), $user);
return $user;
}
/*
* (non-PHPdoc)
* @see \Lithe\Database\Repository::update()
*/
public function update($id, array $attributes)
{
// Flush cache
if ($this->cache->has(md5('id.' . $id))) {
$this->cache->delete(md5('id.' . $id));
}
return $this->repo->update($id, $attributes);
}
}
<?php
namespace Modules\System\Domain\Page;
use Lithe\Database\CacheDecorator;
class PageCache extends CacheDecorator implements PageRepositoryInterface
{
/**
* Fine the current root page
*
* @return Page
*/
public function getRootPage()
{
return $this->cache->forever('root', function () {
return $this->repo->getRootPage();
});
}
/**
* Find a page by its route
*
* @param string $route
*/
public function findByRoute($route)
{
return $this->cache->forever('route', function () use($route) {
return $this->repo->findByRoute($route);
});
}
}
<?php
namespace Modules\System\Domain\Page;
use Lithe\Database\Repository;
class PageRepository extends Repository implements PageRepositoryInterface
{
/**
* The model class
*
* @var string
*/
protected $class = 'Modules\System\Domain\Page\Page';
/**
* (non-PHPdoc)
*
* @see \Lithe\Database\Repository::all()
*/
public function all(array $columns = ['*'])
{
return $this->model->with('layouts.blocks')->get();
}
/**
* Get the root page
*
* @return mixed null|object
*/
public function getRootPage()
{
return $this->model->with('layouts.blocks')->where('is_root', 1)->first();
}
/**
* Find a page by its route
*
* @param string $route
* @return mixed null|object
*/
public function findByRoute($route)
{
return $this->model->with('layouts.blocks')->where('route', $route)->first();
}
}
<?php
namespace Lithe\Database;
use Lithe\Contracts\Database\RepositoryInterface;
use Illuminate\Cache\CacheManager;
abstract class Repository implements RepositoryInterface
{
/**
* The eloquent object
*
* @var object
*/
protected $model;
/**
* The models class
*
* @var string
*/
protected $class;
/**
* Do we cache this module?
*
* @var boolean
*/
protected $cache;
/**
*
* @param Container $app
*/
public function __construct(CacheManager $cache)
{
$this->model = app($this->class);
$this->cache = $cache;
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\RepositoryInterface::create()
*/
public function create(array $attributes)
{
return $this->model->create($attributes);
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\RepositoryInterface::update()
*/
public function update($clause, array $attributes)
{
$info = (! is_array($clause) ? [
'id',
$clause
] : (array) $clause);
return $this->model->where($info[0], $info[1])->update($attributes);
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\RepositoryInterface::all()
*/
public function all(array $columns = ['*'])
{
return $this->model->all($columns);
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\RepositoryInterface::find()
*/
public function find($id, array $columns = ['*'])
{
return $this->model->find($id, $columns);
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\RepositoryInterface::paginate()
*/
public function paginate($perPage = null, array $columns = ['*'])
{
return $this->model->paginate(($perPage ? $perPage : 15));
}
/**
* (non-PHPdoc)
*
* @see \Lithe\Contracts\Database\RepositoryInterface::delete()
*/
public function delete($ids)
{
$this->model->destroy($ids);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment