Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
die_coolen_jungs
our_own_cloud_project
Commits
56ba252e
Commit
56ba252e
authored
Mar 13, 2016
by
Robin McCorkell
Committed by
Thomas Müller
Mar 21, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Redis Cluster support
parent
53c0129e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
40 deletions
+79
-40
config/config.sample.php
config/config.sample.php
+27
-3
lib/private/Memcache/Redis.php
lib/private/Memcache/Redis.php
+14
-14
lib/private/RedisFactory.php
lib/private/RedisFactory.php
+38
-23
No files found.
config/config.sample.php
View file @
56ba252e
...
...
@@ -931,19 +931,43 @@ $CONFIG = array(
'memcache.distributed'
=>
'\OC\Memcache\Memcached'
,
/**
* Connection details for redis to use for memory caching.
* Connection details for redis to use for memory caching
in a single server configuration
.
*
* For enhanced security it is recommended to configure Redis
* to require a password. See http://redis.io/topics/security
* for more information.
*/
'redis'
=>
array
(
'redis'
=>
[
'host'
=>
'localhost'
,
// can also be a unix domain socket: '/tmp/redis.sock'
'port'
=>
6379
,
'timeout'
=>
0.0
,
'password'
=>
''
,
// Optional, if not defined no password will be used.
'dbindex'
=>
0
,
// Optional, if undefined SELECT will not run and will use Redis Server's default DB Index.
),
],
/**
* Connection details for a Redis Cluster
*
* Only for use with Redis Clustering, for Sentinel-based setups use the single
* server configuration above, and perform HA on the hostname.
*
* Redis Cluster support requires the php module phpredis in version 3.0.0 or higher.
*
* Available failover modes:
* - \RedisCluster::FAILOVER_NONE - only send commands to master nodes (default)
* - \RedisCluster::FAILOVER_ERROR - failover to slaves for read commands if master is unavailable
* - \RedisCluster::FAILOVER_DISTRIBUTE - randomly distribute read commands across master and slaves
*/
'redis.cluster'
=>
[
'seeds'
=>
[
// provide some/all of the cluster servers to bootstrap discovery, port required
'localhost:7000'
,
'localhost:7001'
],
'timeout'
=>
0.0
,
'read_timeout'
=>
0.0
,
'failover_mode'
=>
\
RedisCluster
::
FAILOVER_DISTRIBUTE
],
/**
* Server details for one or more memcached servers to use for memory caching.
...
...
lib/private/Memcache/Redis.php
View file @
56ba252e
...
...
@@ -48,8 +48,8 @@ class Redis extends Cache implements IMemcacheTTL {
}
public
function
get
(
$key
)
{
$result
=
self
::
$cache
->
get
(
$this
->
getName
s
pace
()
.
$key
);
if
(
$result
===
false
&&
!
self
::
$cache
->
exists
(
$this
->
getName
s
pace
()
.
$key
))
{
$result
=
self
::
$cache
->
get
(
$this
->
getName
S
pace
()
.
$key
);
if
(
$result
===
false
&&
!
self
::
$cache
->
exists
(
$this
->
getName
S
pace
()
.
$key
))
{
return
null
;
}
else
{
return
json_decode
(
$result
,
true
);
...
...
@@ -58,18 +58,18 @@ class Redis extends Cache implements IMemcacheTTL {
public
function
set
(
$key
,
$value
,
$ttl
=
0
)
{
if
(
$ttl
>
0
)
{
return
self
::
$cache
->
setex
(
$this
->
getName
s
pace
()
.
$key
,
$ttl
,
json_encode
(
$value
));
return
self
::
$cache
->
setex
(
$this
->
getName
S
pace
()
.
$key
,
$ttl
,
json_encode
(
$value
));
}
else
{
return
self
::
$cache
->
set
(
$this
->
getName
s
pace
()
.
$key
,
json_encode
(
$value
));
return
self
::
$cache
->
set
(
$this
->
getName
S
pace
()
.
$key
,
json_encode
(
$value
));
}
}
public
function
hasKey
(
$key
)
{
return
self
::
$cache
->
exists
(
$this
->
getName
s
pace
()
.
$key
);
return
self
::
$cache
->
exists
(
$this
->
getName
S
pace
()
.
$key
);
}
public
function
remove
(
$key
)
{
if
(
self
::
$cache
->
delete
(
$this
->
getName
s
pace
()
.
$key
))
{
if
(
self
::
$cache
->
delete
(
$this
->
getName
S
pace
()
.
$key
))
{
return
true
;
}
else
{
return
false
;
...
...
@@ -77,7 +77,7 @@ class Redis extends Cache implements IMemcacheTTL {
}
public
function
clear
(
$prefix
=
''
)
{
$prefix
=
$this
->
getName
s
pace
()
.
$prefix
.
'*'
;
$prefix
=
$this
->
getName
S
pace
()
.
$prefix
.
'*'
;
$it
=
null
;
self
::
$cache
->
setOption
(
\
Redis
::
OPT_SCAN
,
\
Redis
::
SCAN_RETRY
);
while
(
$keys
=
self
::
$cache
->
scan
(
$it
,
$prefix
))
{
...
...
@@ -110,7 +110,7 @@ class Redis extends Cache implements IMemcacheTTL {
* @return int | bool
*/
public
function
inc
(
$key
,
$step
=
1
)
{
return
self
::
$cache
->
incrBy
(
$this
->
getName
s
pace
()
.
$key
,
$step
);
return
self
::
$cache
->
incrBy
(
$this
->
getName
S
pace
()
.
$key
,
$step
);
}
/**
...
...
@@ -124,7 +124,7 @@ class Redis extends Cache implements IMemcacheTTL {
if
(
!
$this
->
hasKey
(
$key
))
{
return
false
;
}
return
self
::
$cache
->
decrBy
(
$this
->
getName
s
pace
()
.
$key
,
$step
);
return
self
::
$cache
->
decrBy
(
$this
->
getName
S
pace
()
.
$key
,
$step
);
}
/**
...
...
@@ -139,10 +139,10 @@ class Redis extends Cache implements IMemcacheTTL {
if
(
!
is_int
(
$new
))
{
$new
=
json_encode
(
$new
);
}
self
::
$cache
->
watch
(
$this
->
getName
s
pace
()
.
$key
);
self
::
$cache
->
watch
(
$this
->
getName
S
pace
()
.
$key
);
if
(
$this
->
get
(
$key
)
===
$old
)
{
$result
=
self
::
$cache
->
multi
()
->
set
(
$this
->
getName
s
pace
()
.
$key
,
$new
)
->
set
(
$this
->
getName
S
pace
()
.
$key
,
$new
)
->
exec
();
return
(
$result
===
false
)
?
false
:
true
;
}
...
...
@@ -158,10 +158,10 @@ class Redis extends Cache implements IMemcacheTTL {
* @return bool
*/
public
function
cad
(
$key
,
$old
)
{
self
::
$cache
->
watch
(
$this
->
getName
s
pace
()
.
$key
);
self
::
$cache
->
watch
(
$this
->
getName
S
pace
()
.
$key
);
if
(
$this
->
get
(
$key
)
===
$old
)
{
$result
=
self
::
$cache
->
multi
()
->
del
(
$this
->
getName
s
pace
()
.
$key
)
->
del
(
$this
->
getName
S
pace
()
.
$key
)
->
exec
();
return
(
$result
===
false
)
?
false
:
true
;
}
...
...
@@ -170,7 +170,7 @@ class Redis extends Cache implements IMemcacheTTL {
}
public
function
setTTL
(
$key
,
$ttl
)
{
self
::
$cache
->
expire
(
$this
->
getName
s
pace
()
.
$key
,
$ttl
);
self
::
$cache
->
expire
(
$this
->
getName
S
pace
()
.
$key
,
$ttl
);
}
static
public
function
isAvailable
()
{
...
...
lib/private/RedisFactory.php
View file @
56ba252e
...
...
@@ -39,32 +39,47 @@ class RedisFactory {
}
private
function
create
()
{
$this
->
instance
=
new
\
Redis
();
// TODO allow configuring a RedisArray, see https://github.com/nicolasff/phpredis/blob/master/arrays.markdown#redis-arrays
$config
=
$this
->
config
->
getValue
(
'redis'
,
[]);
if
(
isset
(
$config
[
'host'
]))
{
$host
=
$config
[
'host'
];
}
else
{
$host
=
'127.0.0.1'
;
}
if
(
isset
(
$config
[
'port'
]))
{
$port
=
$config
[
'port'
];
}
else
{
$port
=
6379
;
}
if
(
isset
(
$config
[
'timeout'
]))
{
$timeout
=
$config
[
'timeout'
];
if
(
$config
=
$this
->
config
->
getValue
(
'redis.cluster'
,
[]))
{
if
(
!
class_exists
(
'RedisCluster'
))
{
throw
new
\
Exception
(
'Redis Cluster support is not available'
);
}
// cluster config
$timeout
=
isset
(
$config
[
'timeout'
])
?
$config
[
'timeout'
]
:
null
;
$readTimeout
=
isset
(
$config
[
'read_timeout'
])
?
$config
[
'read_timeout'
]
:
null
;
$this
->
instance
=
new
\
RedisCluster
(
null
,
$config
[
'seeds'
],
$timeout
,
$readTimeout
);
if
(
isset
(
$config
[
'failover_mode'
]))
{
$this
->
instance
->
setOption
(
\
RedisCluster
::
OPT_FAILOVER
,
$config
[
'failover_mode'
]);
}
}
else
{
$timeout
=
0.0
;
// unlimited
}
$this
->
instance
->
connect
(
$host
,
$port
,
$timeout
);
if
(
isset
(
$config
[
'password'
])
&&
$config
[
'password'
]
!==
''
)
{
$this
->
instance
->
auth
(
$config
[
'password'
]);
}
$this
->
instance
=
new
\
Redis
();
$config
=
$this
->
config
->
getValue
(
'redis'
,
[]);
if
(
isset
(
$config
[
'host'
]))
{
$host
=
$config
[
'host'
];
}
else
{
$host
=
'127.0.0.1'
;
}
if
(
isset
(
$config
[
'port'
]))
{
$port
=
$config
[
'port'
];
}
else
{
$port
=
6379
;
}
if
(
isset
(
$config
[
'timeout'
]))
{
$timeout
=
$config
[
'timeout'
];
}
else
{
$timeout
=
0.0
;
// unlimited
}
$this
->
instance
->
connect
(
$host
,
$port
,
$timeout
);
if
(
isset
(
$config
[
'password'
])
&&
$config
[
'password'
]
!==
''
)
{
$this
->
instance
->
auth
(
$config
[
'password'
]);
}
if
(
isset
(
$config
[
'dbindex'
]))
{
$this
->
instance
->
select
(
$config
[
'dbindex'
]);
if
(
isset
(
$config
[
'dbindex'
]))
{
$this
->
instance
->
select
(
$config
[
'dbindex'
]);
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment