Skip to content

Commit 5f4a7e2

Browse files
authored
Merge pull request #4 from moorscode/simplify-code
Remove over complicated code
2 parents f18c3c5 + 40b4f18 commit 5f4a7e2

43 files changed

Lines changed: 2190 additions & 1572 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
vendor/
2-
composer.lock
32

43
# Don't save configuration file
54
config/object-cache.config.php

.travis.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@ branches:
99
matrix:
1010
fast_finish: true
1111
include:
12+
- php: 7.2
13+
env: PHPLINT=1 PHPCS=1
1214
- php: 7.0
1315
env: PHPLINT=1
14-
- php: 5.4
16+
- php: 5.6
1517
env: PHPLINT=1
1618

1719
cache:
1820
directories:
1921
- vendor
2022

23+
before_install:
24+
- phpenv config-rm xdebug.ini || echo 'No xdebug config.'
25+
26+
2127
install:
22-
- phpenv local 5.6
23-
- composer selfupdate 1.0.0 --no-interaction
24-
- composer install --no-interaction
25-
- phpenv local --unset
28+
- if [[ "$PHPCS" != "1" ]]; then composer install --no-dev --no-interaction; fi
29+
- if [[ "$PHPCS" == "1" ]]; then composer install --no-interaction; fi
2630

2731
script:
2832
- if [[ "$PHPLINT" == "1" ]]; then find -L . -path ./vendor -prune -o -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l; fi
33+
- if [[ "$PHPCS" == "1" ]]; then vendor/bin/phpcs -v; fi

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Configure per "group" what cache implementation should be used.
55

66
## Requirements
77
- Composer
8-
- PHP 5.4
8+
- PHP 5.6
99

1010
# Installation
1111

@@ -88,4 +88,4 @@ Thanks to the amazing work done by https://github.com/Nyholm and https://github.
8888
Configuration inspiration taken from the https://wordpress.org/plugins/redis-cache/ and https://github.com/ericmann/Redis-Object-Cache projects.
8989

9090
### Memcached
91-
Configuration inspiration taken from the https://github.com/tollmanz/wordpress-pecl-memcached-object-cache project.
91+
Configuration inspiration taken from the https://github.com/tollmanz/wordpress-pecl-memcached-object-cache project.

api.php

Lines changed: 95 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,38 @@
55
*
66
* @subpackage Cache
77
*/
8+
89
use WPMultiObjectCache\Manager;
910

1011
/**
1112
* Sets up Object Cache Global and assigns it.
1213
*/
13-
function wp_cache_init() {
14-
Manager::initialize();
14+
function wp_cache_init()
15+
{
16+
try {
17+
Manager::initialize();
18+
} catch (Exception $exception) {
19+
trigger_error('Cache could not be initialized.', E_USER_ERROR);
20+
}
1521
}
1622

1723
/**
1824
* Adds data to the cache, if the cache key doesn't already exist.
1925
*
20-
* @param int|string $key The cache key to use for retrieval later.
21-
* @param mixed $data The data to add to the cache.
22-
* @param string $group Optional. The group to add the cache to. Enables the same key
26+
* @param int|string $key The cache key to use for retrieval later.
27+
* @param mixed $data The data to add to the cache.
28+
* @param string $group Optional. The group to add the cache to. Enables the same key
2329
* to be used across groups. Default empty.
24-
* @param int $expire Optional. When the cache data should expire, in seconds.
30+
* @param int $expire Optional. When the cache data should expire, in seconds.
2531
* Default 0 (no expiration).
2632
*
2733
* @return bool False if cache key and group already exist, true on success.
2834
*/
29-
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
30-
$object_cache = Manager::getPool( $group );
35+
function wp_cache_add($key, $data, $group = '', $expire = 0)
36+
{
37+
$object_cache = Manager::getPool($group);
3138

32-
return $object_cache->add( $key, $data, $expire );
39+
return $object_cache->add($key, $data, $expire);
3340
}
3441

3542
/**
@@ -43,33 +50,36 @@ function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
4350
*
4451
* @return true Always returns true.
4552
*/
46-
function wp_cache_close() {
47-
return true;
53+
function wp_cache_close()
54+
{
55+
return true;
4856
}
4957

5058
/**
5159
* Decrements numeric cache item's value.
5260
*
53-
* @param int|string $key The cache key to decrement.
54-
* @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
55-
* @param string $group Optional. The group the key is in. Default empty.
61+
* @param int|string $key The cache key to decrement.
62+
* @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
63+
* @param string $group Optional. The group the key is in. Default empty.
5664
*
5765
* @return false|int False on failure, the item's new value on success.
5866
*/
59-
function wp_cache_decr( $key, $offset = 1, $group = '' ) {
60-
return Manager::getPool( $group )->decrease( $key, $offset );
67+
function wp_cache_decr($key, $offset = 1, $group = '')
68+
{
69+
return Manager::getPool($group)->decrease($key, $offset);
6170
}
6271

6372
/**
6473
* Removes the cache contents matching key and group.
6574
*
66-
* @param int|string $key What the contents in the cache are called.
67-
* @param string $group Optional. Where the cache contents are grouped. Default empty.
75+
* @param int|string $key What the contents in the cache are called.
76+
* @param string $group Optional. Where the cache contents are grouped. Default empty.
6877
*
6978
* @return bool True on successful removal, false on failure.
7079
*/
71-
function wp_cache_delete( $key, $group = '' ) {
72-
return Manager::getPool( $group )->delete( $key );
80+
function wp_cache_delete($key, $group = '')
81+
{
82+
return Manager::getPool($group)->delete($key);
7383
}
7484

7585
/**
@@ -79,95 +89,101 @@ function wp_cache_delete( $key, $group = '' ) {
7989
*
8090
* @return bool False on failure, true on success
8191
*/
82-
function wp_cache_flush( $group = null ) {
83-
// No group supplied, flush everything.
84-
if ( null === $group ) {
85-
return wp_cache_flush_all();
86-
}
92+
function wp_cache_flush($group = null)
93+
{
94+
// No group supplied, flush everything.
95+
if (null === $group) {
96+
return wp_cache_flush_all();
97+
}
8798

88-
// Flush specific group.
89-
return Manager::getPool( $group )->clear();
99+
// Flush specific group.
100+
return Manager::getPool($group)->clear();
90101
}
91102

92103
/**
93104
* Removes all cache items from all pools.
94105
*
95106
* @return bool False on failure, true on success
96107
*/
97-
function wp_cache_flush_all() {
98-
$success = true;
108+
function wp_cache_flush_all()
109+
{
110+
$success = true;
99111

100-
$pools = Manager::getPools();
101-
foreach ( $pools as $pool ) {
102-
// @todo track status per controller for request?
103-
$success = $pool->clear() && $success;
104-
}
112+
$pools = Manager::getPools();
113+
foreach ($pools as $pool) {
114+
// @todo track status per controller for request?
115+
$success = $pool->clear() && $success;
116+
}
105117

106-
return $success;
118+
return $success;
107119
}
108120

109121
/**
110122
* Retrieves the cache contents from the cache by key and group.
111123
*
112-
* @param int|string $key The key under which the cache contents are stored.
113-
* @param string $group Optional. Where the cache contents are grouped. Default empty.
114-
* @param bool $force Optional. Whether to force an update of the local cache from the persistent
124+
* @param int|string $key The key under which the cache contents are stored.
125+
* @param string $group Optional. Where the cache contents are grouped. Default empty.
126+
* @param bool $force Optional. Whether to force an update of the local cache from the persistent
115127
* cache. Default false.
116-
* @param bool $found Optional. Whether the key was found in the cache. Disambiguates a return of false,
128+
* @param bool $found Optional. Whether the key was found in the cache. Disambiguates a return of false,
117129
* a storable value. Passed by reference. Default null.
118130
*
119131
* @return bool|mixed False on failure to retrieve contents or the cache
120132
* contents on success
121133
*/
122-
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
123-
return Manager::getPool( $group )->get( $key, $force, $found );
134+
function wp_cache_get($key, $group = '', $force = false, &$found = null)
135+
{
136+
return Manager::getPool($group)->get($key, $force, $found);
124137
}
125138

126139
/**
127140
* Increment numeric cache item's value
128141
*
129-
* @param int|string $key The key for the cache contents that should be incremented.
130-
* @param int $offset Optional. The amount by which to increment the item's value. Default 1.
131-
* @param string $group Optional. The group the key is in. Default empty.
142+
* @param int|string $key The key for the cache contents that should be incremented.
143+
* @param int $offset Optional. The amount by which to increment the item's value. Default 1.
144+
* @param string $group Optional. The group the key is in. Default empty.
132145
*
133146
* @return false|int False on failure, the item's new value on success.
134147
*/
135-
function wp_cache_incr( $key, $offset = 1, $group = '' ) {
136-
return Manager::getPool( $group )->increase( $key, $offset );
148+
function wp_cache_incr($key, $offset = 1, $group = '')
149+
{
150+
return Manager::getPool($group)->increase($key, $offset);
137151
}
138152

139153
/**
140154
* Replaces the contents of the cache with new data.
141155
*
142-
* @param int|string $key The key for the cache data that should be replaced.
143-
* @param mixed $data The new data to store in the cache.
144-
* @param string $group Optional. The group for the cache data that should be replaced.
156+
* @param int|string $key The key for the cache data that should be replaced.
157+
* @param mixed $data The new data to store in the cache.
158+
* @param string $group Optional. The group for the cache data that should be replaced.
145159
* Default empty.
146-
* @param int $expire Optional. When to expire the cache contents, in seconds.
160+
* @param int $expire Optional. When to expire the cache contents, in seconds.
147161
* Default 0 (no expiration).
148162
*
149163
* @return bool False if original value does not exist, true if contents were replaced
150164
*/
151-
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
152-
return Manager::getPool( $group )->replace( $key, $data, $expire );
165+
function wp_cache_replace($key, $data, $group = '', $expire = 0)
166+
{
167+
return Manager::getPool($group)->replace($key, $data, $expire);
153168
}
154169

155170
/**
156171
* Saves the data to the cache.
157172
*
158173
* Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
159174
*
160-
* @param int|string $key The cache key to use for retrieval later.
161-
* @param mixed $data The contents to store in the cache.
162-
* @param string $group Optional. Where to group the cache contents. Enables the same key
175+
* @param int|string $key The cache key to use for retrieval later.
176+
* @param mixed $data The contents to store in the cache.
177+
* @param string $group Optional. Where to group the cache contents. Enables the same key
163178
* to be used across groups. Default empty.
164-
* @param int $expire Optional. When to expire the cache contents, in seconds.
179+
* @param int $expire Optional. When to expire the cache contents, in seconds.
165180
* Default 0 (no expiration).
166181
*
167182
* @return bool False on failure, true on success
168183
*/
169-
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
170-
return Manager::getPool( $group )->set( $key, $data, $expire );
184+
function wp_cache_set($key, $data, $group = '', $expire = 0)
185+
{
186+
return Manager::getPool($group)->set($key, $data, $expire);
171187
}
172188

173189
/**
@@ -177,33 +193,36 @@ function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
177193
*
178194
* @param int $blog_id Site ID.
179195
*/
180-
function wp_cache_switch_to_blog( $blog_id ) {
181-
Manager::switchToBlog( $blog_id );
196+
function wp_cache_switch_to_blog($blog_id)
197+
{
198+
Manager::switchToBlog($blog_id);
182199
}
183200

184201
/**
185202
* Adds a group or set of groups to the list of global groups.
186203
*
187204
* @param string|array $groups A group or an array of groups to add.
188205
*/
189-
function wp_cache_add_global_groups( $groups ) {
190-
$groups = (array) $groups;
191-
foreach ( $groups as $group ) {
192-
Manager::addGroupAlias( '', $group );
193-
}
206+
function wp_cache_add_global_groups($groups)
207+
{
208+
$groups = (array)$groups;
209+
foreach ($groups as $group) {
210+
Manager::addGroupAlias('', $group);
211+
}
194212
}
195213

196214
/**
197215
* Adds a group or set of groups to the list of non-persistent groups.
198216
*
199217
* @param string|array $groups A group or an array of groups to add.
200218
*/
201-
function wp_cache_add_non_persistent_groups( $groups ) {
202-
// Default cache doesn't persist so nothing to do here.
203-
$groups = (array) $groups;
204-
foreach ( $groups as $group ) {
205-
Manager::addGroupAlias( 'non-persistent', $group );
206-
}
219+
function wp_cache_add_non_persistent_groups($groups)
220+
{
221+
// Default cache doesn't persist so nothing to do here.
222+
$groups = (array)$groups;
223+
foreach ($groups as $group) {
224+
Manager::addGroupAlias('non-persistent', $group);
225+
}
207226
}
208227

209228
/**
@@ -219,10 +238,11 @@ function wp_cache_add_non_persistent_groups( $groups ) {
219238
* recommended outside of unit tests as the performance penalty for using it is
220239
* high.
221240
*
222-
* @since 2.6.0
241+
* @since 2.6.0
223242
* @deprecated 3.5.0 WP_Object_Cache::reset()
224-
* @see WP_Object_Cache::reset()
243+
* @see WP_Object_Cache::reset()
225244
*/
226-
function wp_cache_reset() {
227-
_deprecated_function( __FUNCTION__, '3.5.0' );
245+
function wp_cache_reset()
246+
{
247+
_deprecated_function(__FUNCTION__, '3.5.0');
228248
}

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
"name": "moorscode/wp-multi-object-cache",
33
"description": "WordPress Multi Object Cache allowing for a cache implementation on a group basis.",
44
"type": "wordpress-muplugin",
5-
"require": {
6-
"cache/cache": "^0.4.0",
7-
"predis/predis": "^1.1"
8-
},
95
"license": "GPLv3",
106
"authors": [
117
{
128
"name": "Jip Moors",
139
"email": "jhp.moors@gmail.com"
1410
}
1511
],
12+
"require": {
13+
"php": ">=5.6.0",
14+
"cache/cache": "^1.0.0",
15+
"predis/predis": "^1.1"
16+
},
17+
"require-dev": {
18+
"squizlabs/php_codesniffer": "^3.2"
19+
},
1620
"autoload": {
1721
"psr-4": {
1822
"WPMultiObjectCache\\": "src/"

0 commit comments

Comments
 (0)