Skip to content

Commit

Permalink
Use a caching layer for the Quote repository
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti committed Jan 17, 2015
1 parent b832f21 commit 0ce47bc
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 174 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# SERVICES
# CACHE
CACHE_DRIVER=file

# SERVICES# SERVICES
MAILGUN_DOMAIN=dummy
MAILGUN_SECRET=dummy
MAILGUN_PUBKEY=dummy
Expand Down
58 changes: 3 additions & 55 deletions app/TeenQuotes/Quotes/Console/QuotesPublishCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace TeenQuotes\Quotes\Console;

use Cache, Config, Lang, Log;
use Config, Lang, Log;
use Indatus\Dispatcher\Scheduling\Schedulable;
use Indatus\Dispatcher\Scheduling\ScheduledCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -87,8 +87,8 @@ private function getNbQuotesArgument()
{
if (is_null($this->argument('nb_quotes')))
return Config::get('app.quotes.nbQuotesToPublishPerDay');
else
return $this->argument('nb_quotes');

return $this->argument('nb_quotes');
}

/**
Expand All @@ -114,21 +114,13 @@ public function fire()
// Log this info
$this->log("Published quote #".$quote->id);

$this->incrementCachePublishedForUser($quote->user);

// Send an email to the author
$this->userMailer->send('emails.quotes.published',
$quote->user, // The author of the quote
compact('quote'),
Lang::get('quotes.quotePublishedSubjectEmail')
);
});

$this->updateNumberPublishedQuotes();

$this->forgetPagesStoredInCache();

$this->forgetPublishedQuotesPagesForUser();
}

private function log($string)
Expand All @@ -137,50 +129,6 @@ private function log($string)
Log::info($string);
}

/**
* Update the number of published quotes for a user
* @param TeenQuotes\Users\Models\User $u
*/
private function incrementCachePublishedForUser(User $u)
{
if (Cache::has(User::$cacheNameForNumberQuotesPublished.$u->id))
Cache::increment(User::$cacheNameForNumberQuotesPublished.$u->id);
}

/**
* Update number of published quotes in cache
*/
private function updateNumberPublishedQuotes()
{
if (Cache::has(Quote::$cacheNameNumberPublished))
Cache::increment(Quote::$cacheNameNumberPublished, $this->nbQuotesPublished);
}

/**
* We need to forget pages of quotes that are stored in cache
* where the published quotes should be displayed
*/
private function forgetPagesStoredInCache()
{
$nbPages = ceil($this->nbQuotesPublished / Config::get('app.quotes.nbQuotesPerPage'));

for ($i = 1; $i <= $nbPages; $i++)
Cache::forget(Quote::$cacheNameQuotesAPIPage.$i);
}

/**
* We forgot EVERY published quotes stored in cache for every user
* that has published a quote this time
*/
private function forgetPublishedQuotesPagesForUser()
{
foreach ($this->users as $user)
{
Cache::tags(Quote::getCacheNameForUserAndApproved($user, 'waiting'))->flush();
Cache::tags(Quote::getCacheNameForUserAndApproved($user, 'published'))->flush();
}
}

/**
* Get the console command arguments.
*
Expand Down
32 changes: 0 additions & 32 deletions app/TeenQuotes/Quotes/Models/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,6 @@ class Quote extends Toloquent {
*/
public static $cacheNameNbFavorites = 'nb_favorites_';

/**
* The name of the key to store in cache. Describes the quotes for a given page in API with default pagesize.
* @var string
*/
public static $cacheNameQuotesAPIPage = 'quotes_api_';

/**
* The name of the key to store in cache. Describes the quotes for a given "random" page in API with default pagesize.
* @var string
*/
public static $cacheNameRandomAPIPage = 'quotes_random_api';

/**
* The name of the key to store in cache. Describes the number of quotes that have been published.
* @var string
*/
public static $cacheNameNumberPublished = 'nb_quotes_published';

/**
* @var TeenQuotes\Quotes\Repositories\FavoriteQuoteRepository
*/
Expand Down Expand Up @@ -230,20 +212,6 @@ public function registerViewAction()
}
}

/**
* Get the cache array that describes the tag to retrieve quotes for a given user, by approved
* @param TeenQuotes\Users\Models\User $u
* @param string $approve
* @return array
*/
public static function getCacheNameForUserAndApproved(User $u, $approve)
{
if (! in_array($approve, ['pending', 'refused', 'waiting', 'published']))
throw new InvalidArgumentException("Wrong approved type. Got ".$approve);

return ['quotes', 'user', $u->id, $approve];
}

/**
* Lighten or darken a color from an hexadecimal code
* @author http://stackoverflow.com/questions/3512311/how-to-generate-lighter-darker-color-with-php
Expand Down
19 changes: 13 additions & 6 deletions app/TeenQuotes/Quotes/QuotesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use Illuminate\Support\ServiceProvider;
use TeenQuotes\Quotes\Models\FavoriteQuote;
use TeenQuotes\Quotes\Observers\FavoriteQuoteObserver;
use TeenQuotes\Quotes\Repositories\CachingQuoteRepository;
use TeenQuotes\Quotes\Repositories\DbFavoriteQuoteRepository;
use TeenQuotes\Quotes\Repositories\DbQuoteRepository;
use TeenQuotes\Quotes\Repositories\FavoriteQuoteRepository;
use TeenQuotes\Quotes\Repositories\QuoteRepository;
use TeenQuotes\Tools\Namespaces\NamespaceTrait;

class QuotesServiceProvider extends ServiceProvider {
Expand Down Expand Up @@ -68,17 +73,19 @@ private function registerObserver()
private function registerFavoriteQuoteBindings()
{
$this->app->bind(
$this->getNamespaceRepositories().'FavoriteQuoteRepository',
$this->getNamespaceRepositories().'DbFavoriteQuoteRepository'
FavoriteQuoteRepository::class,
DbFavoriteQuoteRepository::class
);
}

private function registerQuotesBindings()
{
$this->app->bind(
$this->getNamespaceRepositories().'QuoteRepository',
$this->getNamespaceRepositories().'DbQuoteRepository'
);
$this->app->bind(QuoteRepository::class, function()
{
$eloquentRepo = new DbQuoteRepository;

return new CachingQuoteRepository($eloquentRepo);
});
}

private function registerFavoriteQuoteRoutes()
Expand Down
Loading

0 comments on commit 0ce47bc

Please sign in to comment.