Dan Brown

Added caching to settings

...@@ -30,7 +30,10 @@ class CustomFacadeProvider extends ServiceProvider ...@@ -30,7 +30,10 @@ class CustomFacadeProvider extends ServiceProvider
30 }); 30 });
31 31
32 $this->app->bind('setting', function() { 32 $this->app->bind('setting', function() {
33 - return new SettingService($this->app->make('Oxbow\Setting')); 33 + return new SettingService(
34 + $this->app->make('Oxbow\Setting'),
35 + $this->app->make('Illuminate\Contracts\Cache\Repository')
36 + );
34 }); 37 });
35 } 38 }
36 } 39 }
......
1 <?php namespace Oxbow\Services; 1 <?php namespace Oxbow\Services;
2 2
3 use Oxbow\Setting; 3 use Oxbow\Setting;
4 +use Illuminate\Contracts\Cache\Repository as Cache;
4 5
5 /** 6 /**
6 * Class SettingService 7 * Class SettingService
...@@ -13,14 +14,19 @@ class SettingService ...@@ -13,14 +14,19 @@ class SettingService
13 { 14 {
14 15
15 protected $setting; 16 protected $setting;
17 + protected $cache;
18 +
19 + protected $cachePrefix = 'setting-';
16 20
17 /** 21 /**
18 * SettingService constructor. 22 * SettingService constructor.
19 - * @param $setting 23 + * @param Setting $setting
24 + * @param Cache $cache
20 */ 25 */
21 - public function __construct(Setting $setting) 26 + public function __construct(Setting $setting, Cache $cache)
22 { 27 {
23 $this->setting = $setting; 28 $this->setting = $setting;
29 + $this->cache = $cache;
24 } 30 }
25 31
26 /** 32 /**
...@@ -32,17 +38,54 @@ class SettingService ...@@ -32,17 +38,54 @@ class SettingService
32 */ 38 */
33 public function get($key, $default = false) 39 public function get($key, $default = false)
34 { 40 {
35 - $setting = $this->getSettingObjectByKey($key); 41 + $value = $this->getValueFromStore($key, $default);
36 - $value = $setting === null ? null : $setting->value; 42 + return $this->formatValue($value, $default);
43 + }
44 +
45 + /**
46 + * Gets a setting value from the cache or database.
47 + * @param $key
48 + * @param $default
49 + * @return mixed
50 + */
51 + protected function getValueFromStore($key, $default)
52 + {
53 + $cacheKey = $this->cachePrefix . $key;
54 + if ($this->cache->has($cacheKey)) {
55 + return $this->cache->get($cacheKey);
56 + }
57 +
58 + $settingObject = $this->getSettingObjectByKey($key);
59 + if($settingObject !== null) {
60 + $value = $settingObject->value;
61 + $this->cache->forever($cacheKey, $value);
62 + return $value;
63 + }
37 64
65 + return $default;
66 + }
67 +
68 + protected function clearFromCache($key)
69 + {
70 + $cacheKey = $this->cachePrefix . $key;
71 + $this->cache->forget($cacheKey);
72 + }
73 +
74 + /**
75 + * Format a settings value
76 + * @param $value
77 + * @param $default
78 + * @return mixed
79 + */
80 + protected function formatValue($value, $default)
81 + {
38 // Change string booleans to actual booleans 82 // Change string booleans to actual booleans
39 - if($value === 'true') $value = true; 83 + if ($value === 'true') $value = true;
40 - if($value === 'false') $value = false; 84 + if ($value === 'false') $value = false;
41 85
42 // Set to default if empty 86 // Set to default if empty
43 - if($value === '') $value = $default; 87 + if ($value === '') $value = $default;
44 - 88 + return $value;
45 - return $value === null ? $default : $value;
46 } 89 }
47 90
48 /** 91 /**
...@@ -69,6 +112,7 @@ class SettingService ...@@ -69,6 +112,7 @@ class SettingService
69 ]); 112 ]);
70 $setting->value = $value; 113 $setting->value = $value;
71 $setting->save(); 114 $setting->save();
115 + $this->clearFromCache($key);
72 return true; 116 return true;
73 } 117 }
74 118
...@@ -83,6 +127,7 @@ class SettingService ...@@ -83,6 +127,7 @@ class SettingService
83 if ($setting) { 127 if ($setting) {
84 $setting->delete(); 128 $setting->delete();
85 } 129 }
130 + $this->clearFromCache($key);
86 return true; 131 return true;
87 } 132 }
88 133
......