Skip to content

Commit

Permalink
because encryption should be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Nov 15, 2024
1 parent 6c03a78 commit 80793cb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":{"Tests\\Unit\\CacheTest::testCacheProvider":8},"times":{"Tests\\Unit\\CacheTest::testCacheProvider":0.073,"Tests\\Unit\\CacheTest::testCacheKeyMaker":0.001}}
{"version":1,"defects":{"Tests\\Unit\\CacheTest::testCacheProvider":8},"times":{"Tests\\Unit\\CacheTest::testCacheProvider":0.069,"Tests\\Unit\\CacheTest::testCacheKeyMaker":0.001}}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Append to the `cache.php` config file in the stores array:
'table' => 'cache',
'database' => env('CACHE_DATABASE', database_path('cache.sqlite')),
'prefix' => '',
'encrypted' => false,
],
```

Expand Down
5 changes: 4 additions & 1 deletion src/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function boot()
]);

Cache::macro('forgetLike', function ($query) {
app('db')->connection('sqlite_cache')->table('cache')->where('key', 'like', "%{$query}%")->delete();
app('db')->connection('sqlite_cache')
->table('cache')
->where('key', 'like', "%{$query}%")
->delete();

return true;
});
Expand Down
22 changes: 19 additions & 3 deletions src/Stores/SqliteStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ public function putMany(array $values, $seconds)
$expiration = $this->getTime() + $seconds;

foreach ($values as $key => $value) {
if (config('cache.stores.sqlite.encrypted', false)) {
$value = encrypt($this->serialize($value));
} else {
$value = $this->serialize($value);
}

$serializedValues[] = [
'key' => $this->prefix.$key,
'value' => encrypt($this->serialize($value)),
'value' => $value,
'expiration' => $expiration,
];
}
Expand Down Expand Up @@ -71,7 +77,11 @@ public function many(array $keys)

return Arr::map($results, function ($value, $key) use ($values) {
if ($cache = $values->firstWhere('key', $this->prefix.$key)) {
return $this->unserialize(decrypt($cache->value));
if (config('cache.stores.sqlite.encrypted', false)) {
return $this->unserialize(decrypt($cache->value));
} else {
return $this->unserialize($cache->value);
}
}

return $value;
Expand All @@ -85,7 +95,13 @@ public function add($key, $value, $seconds)
}

$key = $this->prefix.$key;
$value = encrypt($this->serialize($value));

if (config('cache.stores.sqlite.encrypted', false)) {
$value = encrypt($this->serialize($value));
} else {
$value = $this->serialize($value);
}

$expiration = $this->getTime() + $seconds;

if (! $this->getConnection() instanceof SqlServerConnection) {
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected function getEnvironmentSetUp($app)
'table' => 'cache',
'database' => database_path('cache.sqlite'),
'prefix' => '',
'encrypted' => true,
]);

$app->make('Illuminate\Contracts\Http\Kernel');
Expand Down

0 comments on commit 80793cb

Please sign in to comment.