Skip to content

Commit 949697b

Browse files
committed
Added config and environment variable to allow removal of database information from cache-key
1 parent eb31752 commit 949697b

File tree

6 files changed

+70
-10
lines changed

6 files changed

+70
-10
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.6.1] - 2019-07-20
8+
### Added
9+
- config and environment variable to allow removal of database information from cache-key.
10+
711
## [0.6.0] - 2019-07-20
812
### Changed
913
- environment variable `MODEL_CACHE_DISABLED` to `MODEL_CACHE_ENABLED` to better conform to standards.

Diff for: config/laravel-model-caching.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55

66
'enabled' => env('MODEL_CACHE_ENABLED', true),
77

8+
'use-database-keying' => env('MODEL_CACHE_USE_DATABASE_KEYING', true),
9+
810
'store' => env('MODEL_CACHE_STORE'),
911
];

Diff for: src/Traits/CachePrefixing.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@ trait CachePrefixing
66
{
77
protected function getCachePrefix() : string
88
{
9-
$cachePrefix = Container::getInstance()
9+
$cachePrefix = "genealabs:laravel-model-caching:";
10+
$useDatabaseKeying = Container::getInstance()
11+
->make("config")
12+
->get("laravel-model-caching.use-database-keying");
13+
14+
if ($useDatabaseKeying) {
15+
$cachePrefix .= $this->getConnectionName() . ":";
16+
$cachePrefix .= $this->getDatabaseName() . ":";
17+
}
18+
19+
$cachePrefix .= Container::getInstance()
1020
->make("config")
1121
->get("laravel-model-caching.cache-prefix", "");
1222

1323
if ($this->model
1424
&& property_exists($this->model, "cachePrefix")
1525
) {
16-
$cachePrefix = $this->model->cachePrefix;
26+
$cachePrefix .= $this->model->cachePrefix . ":";
1727
}
1828

19-
$cachePrefix = $cachePrefix
20-
? "{$cachePrefix}:"
21-
: "";
22-
23-
return "genealabs:laravel-model-caching:"
24-
. $this->getConnectionName() . ":"
25-
. $this->getDatabaseName() . ":"
26-
. $cachePrefix;
29+
return $cachePrefix;
2730
}
2831

2932
protected function getDatabaseName() : string

Diff for: tests/Integration/Traits/CachePrefixingTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\Traits;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class CachePrefixingTest extends IntegrationTestCase
8+
{
9+
public function testDatabaseKeyingEnabled()
10+
{
11+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-first");
12+
$tags = [
13+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
14+
];
15+
16+
$author = (new Author)
17+
->first();
18+
$cachedResults = $this->cache()
19+
->tags($tags)
20+
->get($key)['value'];
21+
$liveResults = (new UncachedAuthor)
22+
->first();
23+
24+
$this->assertEquals($liveResults->pluck("id"), $author->pluck("id"));
25+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
26+
$this->assertNotEmpty($author);
27+
$this->assertNotEmpty($cachedResults);
28+
$this->assertNotEmpty($liveResults);
29+
}
30+
31+
public function testDatabaseKeyingDisabled()
32+
{
33+
config(["laravel-model-caching.use-database-keying" => false]);
34+
$key = sha1("genealabs:laravel-model-caching:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-first");
35+
$tags = ["genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor"];
36+
37+
$author = (new Author)
38+
->first();
39+
$cachedResults = $this->cache()
40+
->tags($tags)
41+
->get($key)['value'];
42+
$liveResults = (new UncachedAuthor)
43+
->first();
44+
45+
$this->assertEquals($liveResults->pluck("id"), $author->pluck("id"));
46+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
47+
$this->assertNotEmpty($author);
48+
$this->assertNotEmpty($cachedResults);
49+
$this->assertNotEmpty($liveResults);
50+
}
51+
}

Diff for: tests/database/baseline.sqlite

8 KB
Binary file not shown.

Diff for: tests/database/testing.sqlite

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)