Skip to content

Commit 3cafbb2

Browse files
committed
Add Cache interface
1 parent 878cf7c commit 3cafbb2

32 files changed

+171
-120
lines changed

featherbb/Controller/Admin/Categories.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use FeatherBB\Core\AdminUtils;
1313
use FeatherBB\Core\Error;
1414
use FeatherBB\Core\Interfaces\Container;
15+
use FeatherBB\Core\Interfaces\Cache as CacheInterface;
1516
use FeatherBB\Core\Interfaces\ForumSettings;
1617
use FeatherBB\Core\Interfaces\Hooks;
1718
use FeatherBB\Core\Interfaces\Input;
@@ -68,7 +69,7 @@ public function edit($req, $res, $args)
6869
}
6970

7071
// Regenerate the quick jump cache
71-
Container::get('cache')->store('quickjump', Cache::quickjump());
72+
CacheInterface::store('quickjump', Cache::quickjump());
7273

7374
return Router::redirect(Router::pathFor('adminCategories'), __('Categories updated redirect'));
7475
}

featherbb/Controller/Admin/Forums.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use FeatherBB\Core\AdminUtils;
1313
use FeatherBB\Core\Error;
14+
use FeatherBB\Core\Interfaces\Cache as CacheInterface;
1415
use FeatherBB\Core\Interfaces\Container;
1516
use FeatherBB\Core\Interfaces\ForumSettings;
1617
use FeatherBB\Core\Interfaces\Hooks;
@@ -47,7 +48,7 @@ public function add()
4748

4849
if ($fid = $this->model->addForum($catId, __('New forum'))) {
4950
// Regenerate the quick jump cache
50-
Container::get('cache')->store('quickjump', Cache::quickjump());
51+
CacheInterface::store('quickjump', Cache::quickjump());
5152

5253
return Router::redirect(Router::pathFor('editForum', ['id' => $fid]), __('Forum added redirect'));
5354
} else {
@@ -106,14 +107,14 @@ public function edit($req, $res, $args)
106107
}
107108

108109
// Regenerate the quick jump cache
109-
Container::get('cache')->store('quickjump', Cache::quickjump());
110+
CacheInterface::store('quickjump', Cache::quickjump());
110111

111112
return Router::redirect(Router::pathFor('editForum', ['id' => $args['id']]), __('Forum updated redirect'));
112113
} elseif (Input::post('revert_perms')) {
113114
$this->model->deletePermissions($args['id']);
114115

115116
// Regenerate the quick jump cache
116-
Container::get('cache')->store('quickjump', Cache::quickjump());
117+
CacheInterface::store('quickjump', Cache::quickjump());
117118

118119
return Router::redirect(Router::pathFor('editForum', ['id' => $args['id']]), __('Perms reverted redirect'));
119120
}
@@ -145,7 +146,7 @@ public function delete($req, $res, $args)
145146
if (Request::isPost()) {
146147
$this->model->deleteForum($args['id']);
147148
// Regenerate the quick jump cache
148-
Container::get('cache')->store('quickjump', Cache::quickjump());
149+
CacheInterface::store('quickjump', Cache::quickjump());
149150

150151
return Router::redirect(Router::pathFor('adminForums'), __('Forum deleted redirect'));
151152
} else { // If the user hasn't confirmed
@@ -172,7 +173,7 @@ public function editPositions($req, $res, $args)
172173
}
173174

174175
// Regenerate the quick jump cache
175-
Container::get('cache')->store('quickjump', Cache::quickjump());
176+
CacheInterface::store('quickjump', Cache::quickjump());
176177

177178
return Router::redirect(Router::pathFor('adminForums'), __('Forums updated redirect'));
178179
}

featherbb/Controller/Admin/Parser.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use FeatherBB\Core\AdminUtils;
1313
use FeatherBB\Core\Error;
14+
use FeatherBB\Core\Interfaces\Cache as CacheInterface;
1415
use FeatherBB\Core\Interfaces\Container;
1516
use FeatherBB\Core\Interfaces\ForumEnv;
1617
use FeatherBB\Core\Interfaces\ForumSettings;
@@ -34,16 +35,16 @@ public function __construct()
3435
throw new Error(__('No permission'), '403');
3536
}
3637

37-
if (!Container::get('cache')->isCached('smilies')) {
38-
Container::get('cache')->store('smilies', Cache::getSmilies());
38+
if (!CacheInterface::isCached('smilies')) {
39+
CacheInterface::store('smilies', Cache::getSmilies());
3940
}
4041
}
4142

4243
public function display($req, $res, $args)
4344
{
4445
Hooks::fire('controller.admin.parser.display');
4546

46-
$smilies = Container::get('cache')->retrieve('smilies');
47+
$smilies = CacheInterface::retrieve('smilies');
4748

4849
if (Input::post('form_sent')) {
4950
// Upload new smiley image to style/img/smilies
@@ -97,7 +98,7 @@ public function display($req, $res, $args)
9798
}
9899
}
99100

100-
Container::get('cache')->store('smilies', $smilies);
101+
CacheInterface::store('smilies', $smilies);
101102
}
102103

103104
return Router::redirect(Router::pathFor('adminParser'), __('save_success'));

featherbb/Controller/Admin/Plugins.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use FeatherBB\Core\AdminUtils;
1313
use FeatherBB\Core\Error;
14+
use FeatherBB\Core\Interfaces\Cache;
1415
use FeatherBB\Core\Interfaces\Container;
1516
use FeatherBB\Core\Interfaces\ForumEnv;
1617
use FeatherBB\Core\Interfaces\ForumSettings;
@@ -55,7 +56,7 @@ public function index($req, $res, $args)
5556
View::addAsset('js', 'style/imports/common.js', ['type' => 'text/javascript']);
5657

5758
$availablePlugins = Lister::getPlugins();
58-
$activePlugins = Container::get('cache')->isCached('activePlugins') ? Container::get('cache')->retrieve('activePlugins') : [];
59+
$activePlugins = Cache::isCached('activePlugins') ? Cache::retrieve('activePlugins') : [];
5960

6061
$officialPlugins = Lister::getOfficialPlugins();
6162

featherbb/Controller/Admin/Updates.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use FeatherBB\Core\CoreAutoUpdater;
1414
use FeatherBB\Core\Database;
1515
use FeatherBB\Core\Error;
16+
use FeatherBB\Core\Interfaces\Cache;
1617
use FeatherBB\Core\Interfaces\Container;
1718
use FeatherBB\Core\Interfaces\ForumEnv;
1819
use FeatherBB\Core\Interfaces\ForumSettings;
@@ -126,7 +127,7 @@ public function upgradePlugins($req, $res, $args)
126127
}
127128

128129
// Reset cache
129-
Container::get('cache')->flush();
130+
Cache::flush();
130131

131132
// Display upgrade results
132133
AdminUtils::generateAdminMenu('updates');
@@ -159,7 +160,7 @@ public function upgradeCore($req, $res, $args)
159160
} else {
160161
$upgradeResults[$key]['message'] = sprintf(__('Successful upgrade message'), ForumEnv::get('FORUM_VERSION'), $coreUpdater->getLatestVersion());
161162
// Reset cache and update core version in database
162-
Container::get('cache')->flush();
163+
Cache::flush();
163164
if (!Database::table('config')->rawExecute('UPDATE `'.ForumSettings::get('db_prefix').'config` SET `conf_value` = :value WHERE `conf_name` = "o_cur_version"', ['value' => ForumEnv::get('FORUM_VERSION')])) {
164165
$coreUpdater->addWarning(__('Could not update core version in database'));
165166
}

featherbb/Controller/Auth.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use FeatherBB\Core\Database as DB;
1313
use FeatherBB\Core\Error;
1414
use FeatherBB\Core\Interfaces\Container;
15+
use FeatherBB\Core\Interfaces\Cache as CacheInterface;
1516
use FeatherBB\Core\Interfaces\ForumEnv;
1617
use FeatherBB\Core\Interfaces\ForumSettings;
1718
use FeatherBB\Core\Interfaces\Hooks;
@@ -63,8 +64,8 @@ public function login($req, $res, $args)
6364

6465
if ($user->group_id == ForumEnv::get('FEATHER_UNVERIFIED')) {
6566
ModelAuth::updateGroup($user->id, ForumSettings::get('o_default_user_group'));
66-
if (!Container::get('cache')->isCached('users_info')) {
67-
Container::get('cache')->store('users_info', Cache::getUsersInfo());
67+
if (!CacheInterface::isCached('users_info')) {
68+
CacheInterface::store('users_info', Cache::getUsersInfo());
6869
}
6970
}
7071

@@ -164,7 +165,7 @@ public function forget($req, $res, $args)
164165

165166
Container::get('email')->send($email, $mailSubject, $curMailMessage);
166167

167-
return Router::redirect(Router::pathFor('home'), __('Forget mail').' <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fmailto%3A%3C%2Fspan%3E%27%3C%2Fspan%3E.Utils%3A%3A%3Cspan+class%3D"pl-en">escape(ForumSettings::get('o_admin_email')).'">'.Utils::escape(ForumSettings::get('o_admin_email')).'</a>.', 200, true, true);
168+
return Router::redirect(Router::pathFor('home'), __('Forget mail').' <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fmailto%3A%3C%2Fspan%3E%27%3C%2Fspan%3E.Utils%3A%3A%3Cspan+class%3D"pl-en">escape(ForumSettings::get('o_admin_email')).'">'.Utils::escape(ForumSettings::get('o_admin_email')).'</a>.', 200);
168169
} else {
169170
throw new Error(__('No email match').' '.Utils::escape($email).'.', 400);
170171
}

featherbb/Controller/Install.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace FeatherBB\Controller;
1111

12+
use FeatherBB\Core\Interfaces\Cache;
1213
use FeatherBB\Core\Interfaces\Container;
1314
use FeatherBB\Core\Interfaces\ForumEnv;
1415
use FeatherBB\Core\Interfaces\Hooks;
@@ -48,7 +49,7 @@ public function __construct()
4849

4950
public function run()
5051
{
51-
Container::get('cache')->flush();
52+
Cache::flush();
5253
Hooks::fire('controller.install.run_install');
5354

5455
// First form has been submitted to change default language
@@ -228,7 +229,7 @@ public function createDb(array $data)
228229
Container::get('perms')->allowGroup(4, ['board.read', 'users.view', 'search.topics', 'search.users', 'topic.reply', 'topic.post', 'topic.delete', 'post.delete', 'post.edit', 'post.links', 'email.send']);
229230
Container::get('perms')->allowGroup(2, ['board.read', 'users.view', 'user.set_title', 'search.topics', 'search.users', 'topic.reply', 'topic.post', 'topic.delete', 'post.delete', 'post.edit', 'post.links', 'email.send', 'mod.is_mod', 'mod.edit_users', 'mod.rename_users', 'mod.change_passwords', 'mod.promote_users', 'mod.ban_users']);
230231
Container::get('perms')->allowGroup(1, ['*']);
231-
Container::get('cache')->store('permissions', \FeatherBB\Model\Cache::getPermissions());
232+
Cache::store('permissions', \FeatherBB\Model\Cache::getPermissions());
232233
// Init preferences
233234
Container::get('prefs')->set([
234235
'disp.topics' => 30,

featherbb/Core/AdminUtils.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace FeatherBB\Core;
1111

12+
use FeatherBB\Core\Interfaces\Cache;
1213
use FeatherBB\Core\Interfaces\Container;
1314
use FeatherBB\Core\Interfaces\Lang;
1415
use FeatherBB\Core\Interfaces\View;
@@ -83,11 +84,11 @@ public static function deleteFolder($dirPath)
8384
*/
8485
public static function getAdminIds()
8586
{
86-
if (!Container::get('cache')->isCached('admin_ids')) {
87-
Container::get('cache')->store('admin_ids', \FeatherBB\Model\Cache::getAdminIds());
87+
if (!Cache::isCached('admin_ids')) {
88+
Cache::store('admin_ids', \FeatherBB\Model\Cache::getAdminIds());
8889
}
8990

90-
return Container::get('cache')->retrieve('admin_ids');
91+
return Cache::retrieve('admin_ids');
9192
}
9293

9394
/**
Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: Adrien
5-
* Date: 22/02/2017
6-
* Time: 12:16
7-
*/
2+
namespace FeatherBB\Core\Interfaces;
83

9-
namespace Core\Interfaces;
104

11-
12-
class Cache
5+
class Cache extends SlimSugar
136
{
7+
/**
8+
* Store data in the cache
9+
*
10+
* @param string $key
11+
* @param mixed $data
12+
* @param integer [optional] $expires
13+
* @return self
14+
*/
15+
public static function store($key, $data, $expires = 0)
16+
{
17+
return static::$slim->getContainer()['cache']->store($key, $data, $expires);
18+
}
19+
20+
/**
21+
* Retrieve cached data by key
22+
*
23+
* @param string $key
24+
* @param boolean [optional] $timestamp
25+
* @return string
26+
*/
27+
public static function retrieve($key)
28+
{
29+
return static::$slim->getContainer()['cache']->retrieve($key);
30+
}
31+
32+
/**
33+
* Check whether data is associated with a key
34+
*
35+
* @param string $key
36+
* @return boolean
37+
*/
38+
public static function isCached($key)
39+
{
40+
return static::$slim->getContainer()['cache']->isCached($key);
41+
}
1442

43+
/**
44+
* Flush all cached entries
45+
* @return object
46+
*/
47+
public static function flush()
48+
{
49+
return static::$slim->getContainer()['cache']->flush();
50+
}
1551
}

featherbb/Core/Interfaces/SlimStatic.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public static function boot(\Slim\App $slim)
1818
$manager = new \Statical\Manager();
1919

2020
// Add proxies that use the Slim instance
21-
$aliases = ['Feather', 'Config', 'Route', 'Router', 'ForumEnv', 'ForumSettings', 'User', 'Lang', 'Hooks'];
21+
$aliases = ['Feather', 'Config', 'Route', 'Router', 'ForumEnv',
22+
'ForumSettings', 'User', 'Lang', 'Hooks', 'Cache'];
2223
static::addInstances($aliases, $manager, $slim);
2324

2425
// Add special-case Slim container instance

0 commit comments

Comments
 (0)