Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Nextcloud
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Nextcloud
Commits
1bc56a99
Commit
1bc56a99
authored
9 years ago
by
Robin Appelman
Browse files
Options
Downloads
Patches
Plain Diff
compare-and-set and compare-and-delete using lua scripts for redis
parent
446f6281
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
lib/private/memcache/redis.php
+50
-2
50 additions, 2 deletions
lib/private/memcache/redis.php
with
50 additions
and
2 deletions
lib/private/memcache/redis.php
+
50
−
2
View file @
1bc56a99
...
@@ -26,13 +26,15 @@ namespace OC\Memcache;
...
@@ -26,13 +26,15 @@ namespace OC\Memcache;
use
OCP\IMemcache
;
use
OCP\IMemcache
;
class
Redis
extends
Cache
implements
IMemcache
{
class
Redis
extends
Cache
implements
IMemcache
{
use
CASTrait
;
/**
/**
* @var \Redis $cache
* @var \Redis $cache
*/
*/
private
static
$cache
=
null
;
private
static
$cache
=
null
;
private
static
$casScript
;
private
static
$cadScript
;
public
function
__construct
(
$prefix
=
''
)
{
public
function
__construct
(
$prefix
=
''
)
{
parent
::
__construct
(
$prefix
);
parent
::
__construct
(
$prefix
);
if
(
is_null
(
self
::
$cache
))
{
if
(
is_null
(
self
::
$cache
))
{
...
@@ -57,6 +59,29 @@ class Redis extends Cache implements IMemcache {
...
@@ -57,6 +59,29 @@ class Redis extends Cache implements IMemcache {
self
::
$cache
->
connect
(
$host
,
$port
,
$timeout
);
self
::
$cache
->
connect
(
$host
,
$port
,
$timeout
);
self
::
$casScript
=
self
::
$cache
->
script
(
'load'
,
'
local key = ARGV[1]
local old = ARGV[2]
local new = ARGV[3]
if redis.call(\'get\', key) == old then
redis.call(\'set\', key, new)
return true
end
return false'
);
self
::
$cadScript
=
self
::
$cache
->
script
(
'load'
,
'
local key = ARGV[1]
local old = ARGV[2]
if redis.call(\'get\', key) == old then
redis.call(\'del\', key)
return true
end
return false'
);
if
(
isset
(
$config
[
'dbindex'
]))
{
if
(
isset
(
$config
[
'dbindex'
]))
{
self
::
$cache
->
select
(
$config
[
'dbindex'
]);
self
::
$cache
->
select
(
$config
[
'dbindex'
]);
}
}
...
@@ -150,6 +175,29 @@ class Redis extends Cache implements IMemcache {
...
@@ -150,6 +175,29 @@ class Redis extends Cache implements IMemcache {
return
self
::
$cache
->
decrBy
(
$this
->
getNamespace
()
.
$key
,
$step
);
return
self
::
$cache
->
decrBy
(
$this
->
getNamespace
()
.
$key
,
$step
);
}
}
/**
* Compare and set
*
* @param string $key
* @param mixed $old
* @param mixed $new
* @return bool
*/
public
function
cas
(
$key
,
$old
,
$new
)
{
return
(
bool
)
self
::
$cache
->
evalSha
(
self
::
$casScript
,
[
$this
->
getNamespace
()
.
$key
,
json_encode
(
$old
),
json_encode
(
$new
)]);
}
/**
* Compare and delete
*
* @param string $key
* @param mixed $old
* @return bool
*/
public
function
cad
(
$key
,
$old
)
{
return
(
bool
)
self
::
$cache
->
evalSha
(
self
::
$cadScript
,
[
$this
->
getNamespace
()
.
$key
,
json_encode
(
$old
)]);
}
static
public
function
isAvailable
()
{
static
public
function
isAvailable
()
{
return
extension_loaded
(
'redis'
)
return
extension_loaded
(
'redis'
)
&&
version_compare
(
phpversion
(
'redis'
),
'2.2.5'
,
'>='
);
&&
version_compare
(
phpversion
(
'redis'
),
'2.2.5'
,
'>='
);
...
...
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