Skip to content

Commit

Permalink
added cache decorator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fideloper committed Oct 13, 2013
1 parent f73aeb8 commit 4e2146d
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/Impl/Repo/Article/AbstractArticleDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php namespace Impl\Repo\Article;

abstract class AbstractArticleDecorator implements ArticleInterface {

protected $nextArticle;

public function __construct(ArticleInterface $article)
{
$this->nextArticle = $article;
}

/**
* {@inheritdoc}
*/
abstract public function byId($id);

/**
* {@inheritdoc}
*/
abstract public function byPage($page=1, $limit=10, $all=false);

/**
* {@inheritdoc}
*/
abstract public function bySlug($slug);

/**
* {@inheritdoc}
*/
abstract public function byTag($tag, $page=1, $limit=10);

/**
* {@inheritdoc}
*/
abstract public function create(array $data);

/**
* {@inheritdoc}
*/
abstract public function update(array $data);

}
128 changes: 128 additions & 0 deletions app/Impl/Repo/Article/CacheDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php namespace Impl\Repo\Article;

use Impl\Service\Cache\CacheInterface;

class CacheDecorator extends AbstractArticleDecorator {

protected $nextArticle;
protected $cache;

public function __construct(ArticleInterface $article, CacheInterface $cache)
{
$this->nextArticle = $article;
$this->cache = $cache;
}

/**
* Attempt to retrieve from cache
* by ID
* {@inheritdoc}
*/
public function byId($id)
{
$key = md5('id.'.$id);

if( $this->cache->has($key) )
{
return $this->cache->get($key);
}

$article = $this->nextArticle->byId($id);

$this->cache->put($key, $article);

return $article;
}

/**
* Attempt to retrieve from cache
* {@inheritdoc}
*/
public function byPage($page=1, $limit=10, $all=false)
{
$allkey = ($all) ? '.all' : '';
$key = md5('page.'.$page.'.'.$limit.$allkey);

if( $this->cache->has($key) )
{
return $this->cache->get($key);
}

$articles = $this->nextArticle->byPage($page, $limit);

$cached = $this->cache->putPaginated(
$page,
$limit,
$this->nextArticle->totalArticles($all),
$articles->all(),
$key
);

return $cached;
}

/**
* Attempt to retrieve from cache
* {@inheritdoc}
*/
public function bySlug($slug)
{
$key = md5('slug.'.$slug);

if( $this->cache->has($key) )
{
return $this->cache->get($key);
}

$article = $this->nextArticle->bySlug($slug);

$this->cache->put($key, $article);

return $article;
}

/**
* Attempt to retrieve from cache
* {@inheritdoc}
*/
public function byTag($tag, $page=1, $limit=10)
{
$key = md5('tag.'.$tag.'.'.$page.'.'.$limit);

if( $this->cache->has($key) )
{
return $this->cache->get($key);
}

$articles = $this->nextArticle->byId($tag, $page, $limit);

$cached = $this->cache->put(
$page,
$limit,
$this->nextArticle->totalByTag($tag),
$articles->all(),
$key
);

return $cached;
}

/**
* Pass creation of article thru
* {@inheritdoc}
*/
public function create(array $data)
{
return $this->nextArticle->byId($data);
}

/**
* Pass update of article thru
* {@inheritdoc}
*/
public function update(array $data)
{
return $this->nextArticle->update($data);
}

}

0 comments on commit 4e2146d

Please sign in to comment.