-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from 1FF/implement-cache-tags
Implement cache tags
- Loading branch information
Showing
8 changed files
with
180 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace ForFit\Mongodb\Cache\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
use \MongoDB\Driver\ReadPreference; | ||
|
||
/** | ||
* Create indexes for the cache collection | ||
*/ | ||
class MongodbCacheIndexTags extends Command | ||
{ | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'mongodb:cache:index_tags'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create indexes on the tags column of mongodb `cache` collection'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$cacheCollectionName = config('cache')['stores']['mongodb']['table']; | ||
|
||
DB::connection('mongodb')->getMongoDB()->command([ | ||
'createIndexes' => $cacheCollectionName, | ||
'indexes' => [ | ||
[ | ||
'key' => ['tags' => 1], | ||
'name' => 'tags_1', | ||
'background' => true | ||
], | ||
] | ||
], [ | ||
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY) | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Database/Migrations/2019_01_31_000000_add_index_to_tags_column.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\Artisan; | ||
|
||
class AddIndexToTagsColumn extends Migration | ||
{ | ||
|
||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Artisan::call('mongodb:cache:index_tags'); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Artisan::call('mongodb:cache:dropindex', ['index' => 'tags']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace ForFit\Mongodb\Cache; | ||
|
||
use Illuminate\Cache\Repository; | ||
use Illuminate\Contracts\Cache\Store; | ||
use Illuminate\Cache\Events\KeyWritten; | ||
|
||
class MongoTaggedCache extends Repository | ||
{ | ||
protected $tags; | ||
|
||
/** | ||
* @param \Illuminate\Contracts\Cache\Store $store | ||
* @param array $tags | ||
*/ | ||
public function __construct(Store $store, array $tags = []) | ||
{ | ||
parent::__construct($store); | ||
|
||
$this->tags = $tags; | ||
} | ||
|
||
/** | ||
* Store an item in the cache. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @param \DateTimeInterface|\DateInterval|float|int $minutes | ||
* @return void | ||
*/ | ||
public function put($key, $value, $minutes = null) | ||
{ | ||
if (is_array($key)) { | ||
return $this->putMany($key, $value); | ||
} | ||
|
||
if (! is_null($minutes = $this->getMinutes($minutes))) { | ||
$this->store->put($this->itemKey($key), $value, $minutes, $this->tags); | ||
|
||
$this->event(new KeyWritten($key, $value, $minutes)); | ||
} | ||
} | ||
|
||
/** | ||
* Saves array of key value pairs to the cache | ||
* | ||
* @param array $values | ||
* @param \DateTimeInterface|\DateInterval|float|int $minutes | ||
* @return void | ||
*/ | ||
public function putMany(array $values, $minutes) | ||
{ | ||
foreach ($values as $key => $value) { | ||
$this->put($key, $value, $minutes); | ||
} | ||
} | ||
|
||
/** | ||
* Flushes the cache for the given tags | ||
* | ||
* @return void | ||
*/ | ||
public function flush() | ||
{ | ||
return $this->store->flushByTags($this->tags); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters