Skip to content

Commit

Permalink
Merge pull request #120 from Vendin/cache
Browse files Browse the repository at this point in the history
Cache for read settings from store
  • Loading branch information
bweston92 authored Sep 28, 2019
2 parents 44e8d1a + 733ec65 commit 650a92e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ In Laravel 4.x, the library makes sure to auto-save every time the application s
In Laravel 5.x, if you add the middleware `anlutro\LaravelSettings\SaveMiddleware` to your `middleware` list in `app\Http\Kernel.php`, settings will be saved automatically at the end of all HTTP requests, but you'll still need to call `Setting::save()` explicitly in console commands, queue workers etc.


### Store cache

When reading from the store, you can enable the cache.

You can also configure flushing of the cache when writing and configure time to live.

Reading will come from the store, and then from the cache, this can reduce load on the store.

```php
// Cache usage configurations.
'enableCache' => false,
'forgetCacheByWrite' => true,
'cacheTtl' => 15,
```

### JSON storage

You can modify the path used on run-time using `Setting::setPath($path)`.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": ">=5.4.0",
"illuminate/support": ">=4.1 <7.0",
"illuminate/cache": ">=4.1 <7.0",
"laravel/helpers": ">=1.0.0"
},
"suggest": {
Expand Down
26 changes: 25 additions & 1 deletion src/SettingStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

namespace anlutro\LaravelSettings;

use Illuminate\Support\Facades\Cache;
use \Illuminate\Support\Facades\Config;

abstract class SettingStore
{
/**
* Cache key for save
*/
const CACHE_KEY = 'setting:cache';

/**
* The settings data.
*
Expand Down Expand Up @@ -137,6 +143,10 @@ public function save()
return;
}

if (Config::get('settings.forgetCacheByWrite')) {
Cache::forget(static::CACHE_KEY);
}

$this->write($this->data);
$this->unsaved = false;
}
Expand All @@ -149,11 +159,25 @@ public function save()
public function load($force = false)
{
if (!$this->loaded || $force) {
$this->data = $this->read();
$this->data = $this->readData();
$this->loaded = true;
}
}

/**
* Read data from a store or cache
*
* @return array
*/
private function readData() {
if (Config::get('settings.enableCache')) {
return Cache::remember(static::CACHE_KEY, Config::get('settings.cacheTtl'), function () {
return $this->read();
});
}
return $this->read();
}

/**
* Read the data from the store.
*
Expand Down
6 changes: 5 additions & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
'connection' => null,
// Name of the table used.
'table' => 'settings',
// If you want to use custom column names in database store you could
// Cache usage configurations.
'enableCache' => false,
'forgetCacheByWrite' => true,
'cacheTtl' => 15,
// If you want to use custom column names in database store you could
// set them in this configuration
'keyColumn' => 'key',
'valueColumn' => 'value',
Expand Down
10 changes: 9 additions & 1 deletion tests/functional/AbstractFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?php

use anlutro\LaravelSettings\JsonSettingStore;
use anlutro\LaravelSettings\DatabaseSettingStore;
use Illuminate\Support\Facades\Config;

abstract class AbstractFunctionalTest extends PHPUnit_Framework_TestCase
{
protected abstract function createStore(array $data = array());

public function setUp()
{
Config::shouldReceive('get')
->with('settings.enableCache')
->andReturn(false)
->getMock();
}

protected function assertStoreEquals($store, $expected, $message = null)
{
$this->assertEquals($expected, $store->all(), $message);
Expand Down
1 change: 1 addition & 0 deletions tests/functional/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function setUp()
$t->string('key', 64)->unique();
$t->string('value', 4096);
});
parent::setUp();
}

public function tearDown()
Expand Down

0 comments on commit 650a92e

Please sign in to comment.