Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cache/defaults not working with custom stores #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ Setting::setConstraint(function($query, $insert) {

### Custom stores

This package uses the Laravel `Manager` class under the hood, so it's easy to add your own custom session store driver if you want to store in some other way. All you need to do is extend the abstract `SettingStore` class, implement the abstract methods and call `Setting::extend`.
This package uses the Laravel `Manager` class under the hood, so it's easy to add your own custom session store driver if you want to store in some other way.
All you need to do is extend the abstract `CachedSettingStore` class, implement the abstract methods and call `Setting::extend`.

```php
<?php
class MyStore extends anlutro\LaravelSettings\SettingStore {
class MyStore extends anlutro\LaravelSettings\CachedSettingStore {
// ...
}
Setting::extend('mystore', function($app) {
Expand All @@ -138,6 +139,7 @@ Setting::extend('mystore', function($app) {
?>
```

If you want your setting store to be exempt from Laravel's caching, extend from `SettingStore` instead.

## Contact

Expand Down
85 changes: 85 additions & 0 deletions src/CachedSettingStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Laravel 4 - Persistent Settings
*
* @author Andreas Lutro <[email protected]>
* @license http://opensource.org/licenses/MIT
* @package l4-settings
*/

namespace anlutro\LaravelSettings;

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

/**
* @var \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface
*/
protected $cache = null;

/**
* Cache TTL in seconds.
*
* @var int
*/
protected $cacheTtl = 15;

/**
* Whether to reset the cache when changing a setting.
*
* @var boolean
*/
protected $cacheForgetOnWrite = true;

/**
* Set the cache.
* @param \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface $cache
* @param int $ttl
* @param bool $forgetOnWrite
*/
public function setCache($cache, $ttl = null, $forgetOnWrite = null)
{
$this->cache = $cache;
if ($ttl !== null) {
$this->cacheTtl = $ttl;
}
if ($forgetOnWrite !== null) {
$this->cacheForgetOnWrite = $forgetOnWrite;
}
}

/**
* Save any changes done to the settings data.
*
* @return void
*/
public function save()
{
if ($this->unsaved && $this->cache && $this->cacheForgetOnWrite) {
$this->cache->forget(static::CACHE_KEY);
}

parent::save();
}

/**
* Read data from a store
*
* @return array
*/
private function readData()
{
if ($this->cache) {
return $this->cache->remember(static::CACHE_KEY, $this->cacheTtl, function () {
return $this->read();
});
}

return $this->read();
}

}
2 changes: 1 addition & 1 deletion src/DatabaseSettingStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Illuminate\Database\Connection;
use Illuminate\Support\Arr;

class DatabaseSettingStore extends SettingStore
class DatabaseSettingStore extends CachedSettingStore
{
/**
* The database connection instance.
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSettingStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Illuminate\Filesystem\Filesystem;

class JsonSettingStore extends SettingStore
class JsonSettingStore extends CachedSettingStore
{
/**
* @param \Illuminate\Filesystem\Filesystem $files
Expand Down
54 changes: 2 additions & 52 deletions src/SettingStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

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

/**
* The settings data.
*
Expand Down Expand Up @@ -58,25 +53,6 @@ abstract class SettingStore
*/
protected $defaults = [];

/**
* @var \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface
*/
protected $cache = null;

/**
* Cache TTL in seconds.
*
* @var int
*/
protected $cacheTtl = 15;

/**
* Whether to reset the cache when changing a setting.
*
* @var boolean
*/
protected $cacheForgetOnWrite = true;

/**
* Set default values.
*
Expand All @@ -87,23 +63,6 @@ public function setDefaults(array $defaults)
$this->defaults = $defaults;
}

/**
* Set the cache.
* @param \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface $cache
* @param int $ttl
* @param bool $forgetOnWrite
*/
public function setCache($cache, $ttl = null, $forgetOnWrite = null)
{
$this->cache = $cache;
if ($ttl !== null) {
$this->cacheTtl = $ttl;
}
if ($forgetOnWrite !== null) {
$this->cacheForgetOnWrite = $forgetOnWrite;
}
}

/**
* Get a specific key from the settings data.
*
Expand Down Expand Up @@ -213,10 +172,6 @@ public function save()
return;
}

if ($this->cache && $this->cacheForgetOnWrite) {
$this->cache->forget(static::CACHE_KEY);
}

$this->write($this->data);
$this->unsaved = false;
}
Expand All @@ -237,18 +192,13 @@ public function load($force = false)
}

/**
* Read data from a store or cache
* Read data from a store.
*
* @return array
*/
private function readData()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It always calls this version of the method because it is private. It should be protected here and in CachedSettingStore.

{
if ($this->cache) {
return $this->cache->remember(static::CACHE_KEY, $this->cacheTtl, function () {
return $this->read();
});
}

// note: this method exists purely for CachedSettingStore.
return $this->read();
}

Expand Down
22 changes: 10 additions & 12 deletions src/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public function createJsonDriver()
{
$path = $this->getConfig('anlutro/l4-settings::path');

$store = new JsonSettingStore($this->getSupportedContainer()['files'], $path);

return $this->wrapDriver($store);
return new JsonSettingStore($this->getSupportedContainer()['files'], $path);
}

public function createDatabaseDriver()
Expand All @@ -36,14 +34,12 @@ public function createDatabaseDriver()
$keyColumn = $this->getConfig('anlutro/l4-settings::keyColumn');
$valueColumn = $this->getConfig('anlutro/l4-settings::valueColumn');

$store = new DatabaseSettingStore($connection, $table, $keyColumn, $valueColumn);

return $this->wrapDriver($store);
return new DatabaseSettingStore($connection, $table, $keyColumn, $valueColumn);
}

public function createMemoryDriver()
{
return $this->wrapDriver(new MemorySettingStore());
return new MemorySettingStore();
}

public function createArrayDriver()
Expand All @@ -60,19 +56,21 @@ protected function getConfig($key)
return $this->getSupportedContainer()['config']->get($key);
}

protected function wrapDriver($store)
protected function createDriver($driver)
{
$store->setDefaults($this->getConfig('anlutro/l4-settings::defaults'));
$instance = parent::createDriver($driver);

$instance->setDefaults($this->getConfig('anlutro/l4-settings::defaults'));

if ($this->getConfig('anlutro/l4-settings::enableCache')) {
$store->setCache(
if ($instance instanceof CachedSettingStore && $this->getConfig('anlutro/l4-settings::enableCache')) {
$instance->setCache(
$this->getSupportedContainer()['cache'],
$this->getConfig('anlutro/l4-settings::cacheTtl'),
$this->getConfig('anlutro/l4-settings::forgetCacheByWrite')
);
}

return $store;
return $instance;
}

protected function getSupportedContainer()
Expand Down