Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Redis Object Cache
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
WP Plugins
Redis Object Cache
Commits
bd44ddf5
Commit
bd44ddf5
authored
11 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Implement `wp_cache_get_multi()` to retrieve multiple group-key pairs in a single request.
Fixes
#5
.
parent
214942da
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
object-cache.php
+93
-0
93 additions, 0 deletions
object-cache.php
with
93 additions
and
0 deletions
object-cache.php
+
93
−
0
View file @
bd44ddf5
...
@@ -92,6 +92,23 @@ function wp_cache_get( $key, $group = '' ) {
...
@@ -92,6 +92,23 @@ function wp_cache_get( $key, $group = '' ) {
return
$wp_object_cache
->
get
(
$key
,
$group
);
return
$wp_object_cache
->
get
(
$key
,
$group
);
}
}
/**
* Retrieve multiple values from cache.
*
* Gets multiple values from cache, including across multiple groups
*
* Usage: array( 'group0' => array( 'key0', 'key1', 'key2', ), 'group1' => array( 'key0' ) )
*
* Mirrors the Memcached Object Cache plugin's argument and return-value formats
*
* @param array $groups Array of groups and keys to retrieve
* @return bool|mixed Array of cached values, keys in the format $group:$key. Non-existent keys false
*/
function
wp_cache_get_multi
(
$groups
)
{
global
$wp_object_cache
;
return
$wp_object_cache
->
get_multi
(
$groups
);
}
/**
/**
* Increment a numeric item's value.
* Increment a numeric item's value.
*
*
...
@@ -498,6 +515,65 @@ class WP_Object_Cache {
...
@@ -498,6 +515,65 @@ class WP_Object_Cache {
return
is_object
(
$value
)
?
clone
$value
:
$value
;
return
is_object
(
$value
)
?
clone
$value
:
$value
;
}
}
/**
* Retrieve multiple values from cache.
*
* Gets multiple values from cache, including across multiple groups
*
* Usage: array( 'group0' => array( 'key0', 'key1', 'key2', ), 'group1' => array( 'key0' ) )
*
* Mirrors the Memcached Object Cache plugin's argument and return-value formats
*
* @param array $groups Array of groups and keys to retrieve
* @uses this::filter_redis_get_multi()
* @return bool|mixed Array of cached values, keys in the format $group:$key. Non-existent keys null.
*/
public
function
get_multi
(
$groups
)
{
if
(
empty
(
$groups
)
||
!
is_array
(
$groups
)
)
{
return
false
;
}
// Retrieve requested caches and reformat results to mimic Memcached Object Cache's output
$cache
=
array
();
foreach
(
$groups
as
$group
=>
$keys
)
{
if
(
in_array
(
$group
,
$this
->
no_redis_groups
)
||
!
$this
->
can_redis
()
)
{
foreach
(
$keys
as
$key
)
{
$cache
[
$this
->
build_key
(
$key
,
$group
)
]
=
$this
->
get
(
$key
,
$group
);
}
}
else
{
// Reformat arguments as expected by Redis
$derived_keys
=
array
();
foreach
(
$keys
as
$key
)
{
$derived_keys
[]
=
$this
->
build_key
(
$key
,
$group
);
}
// Retrieve from cache in a single request
$group_cache
=
$this
->
redis
->
mget
(
$derived_keys
);
// Build an array of values looked up, keyed by the derived cache key
$group_cache
=
array_combine
(
$derived_keys
,
$group_cache
);
// Redis returns null for values not found in cache, but expected return value is false in this instance
$group_cache
=
array_map
(
array
(
$this
,
'filter_redis_get_multi'
),
$group_cache
);
$cache
=
array_merge
(
$cache
,
$group_cache
);
}
}
// Add to the internal cache the found values from Redis
foreach
(
$cache
as
$key
=>
$value
)
{
if
(
$value
)
{
$this
->
cache_hits
++
;
$this
->
add_to_internal_cache
(
$key
,
$value
);
}
else
{
$this
->
cache_misses
++
;
}
}
return
$cache
;
}
/**
/**
* Sets a value in cache.
* Sets a value in cache.
*
*
...
@@ -658,6 +734,23 @@ class WP_Object_Cache {
...
@@ -658,6 +734,23 @@ class WP_Object_Cache {
return
$value
;
return
$value
;
}
}
/**
* Convert data types when using Redis MGET
*
* When requesting multiple keys, those not found in cache are assigned the value null upon return.
* Expected value in this case is false, so we convert
*
* @param string $value Value to possibly convert
* @return string Converted value
*/
protected
function
filter_redis_get_multi
(
$value
)
{
if
(
is_null
(
$value
)
)
{
$value
=
false
;
}
return
$value
;
}
/**
/**
* Convert the response fro Predis into something meaningful
* Convert the response fro Predis into something meaningful
*
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment