Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
8d03032a
Commit
8d03032a
authored
Apr 21, 2009
by
antirez
Browse files
PHP client ported to PHP5 and fixed
parent
3c910136
Changes
2
Hide whitespace changes
Inline
Side-by-side
client-libraries/php/redis.php
View file @
8d03032a
...
...
@@ -3,6 +3,7 @@
* Redis PHP Bindings - http://code.google.com/p/redis/
*
* Copyright 2009 Ludovico Magnocavallo
* Copyright 2009 Salvatore Sanfilippo (ported it to PHP5, fixed some bug)
* Released under the same license as Redis.
*
* Version: 0.1
...
...
@@ -14,240 +15,268 @@
class
Redis
{
var
$server
;
var
$port
;
var
$_sock
;
function
Redis
(
$host
=
'localhost'
,
$port
=
6379
)
{
public
$server
;
public
$port
;
private
$_sock
;
public
function
__construct
(
$host
=
'localhost'
,
$port
=
6379
)
{
$this
->
host
=
$host
;
$this
->
port
=
$port
;
}
function
connect
()
{
if
(
$this
->
_sock
)
return
;
public
function
connect
()
{
if
(
$this
->
_sock
)
return
;
if
(
$sock
=
fsockopen
(
$this
->
host
,
$this
->
port
,
$errno
,
$errstr
))
{
$this
->
_sock
=
$sock
;
return
;
}
$msg
=
"Cannot open socket to
{
$this
->
host
}
:
{
$this
->
port
}
"
;
if
(
$errno
||
$errmsg
)
$msg
.
=
","
.
(
$errno
?
" error
$errno
"
:
""
)
.
(
$errmsg
?
"
$errmsg
"
:
""
);
$msg
.
=
","
.
(
$errno
?
" error
$errno
"
:
""
)
.
(
$errmsg
?
"
$errmsg
"
:
""
);
trigger_error
(
"
$msg
."
,
E_USER_ERROR
);
}
function
disconnect
()
{
if
(
$this
->
_sock
)
@
fclose
(
$this
->
_sock
);
public
function
disconnect
()
{
if
(
$this
->
_sock
)
@
fclose
(
$this
->
_sock
);
$this
->
_sock
=
null
;
}
function
&
ping
()
{
public
function
ping
()
{
$this
->
connect
();
$this
->
_
write
(
"PING
\r\n
"
);
$this
->
write
(
"PING
\r\n
"
);
return
$this
->
get_response
();
}
function
&
do_echo
(
$s
)
{
public
function
do_echo
(
$s
)
{
$this
->
connect
();
$this
->
_
write
(
"ECHO "
.
strlen
(
$s
)
.
"
\r\n
$s
\r\n
"
);
return
$this
->
_
get_
valu
e
();
$this
->
write
(
"ECHO "
.
strlen
(
$s
)
.
"
\r\n
$s
\r\n
"
);
return
$this
->
get_
respons
e
();
}
function
&
set
(
$name
,
$value
,
$preserve
=
false
)
{
public
function
set
(
$name
,
$value
,
$preserve
=
false
)
{
$this
->
connect
();
$this
->
_
write
(
$this
->
write
(
(
$preserve
?
'SETNX'
:
'SET'
)
.
"
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
return
$this
->
get_response
();
}
function
&
get
(
$name
)
{
public
function
get
(
$name
)
{
$this
->
connect
();
$this
->
_
write
(
"GET
$name
\r\n
"
);
return
$this
->
_
get_
valu
e
();
$this
->
write
(
"GET
$name
\r\n
"
);
return
$this
->
get_
respons
e
();
}
function
&
incr
(
$name
,
$amount
=
1
)
{
public
function
incr
(
$name
,
$amount
=
1
)
{
$this
->
connect
();
if
(
$amount
==
1
)
$this
->
_
write
(
"INCR
$name
\r\n
"
);
$this
->
write
(
"INCR
$name
\r\n
"
);
else
$this
->
_
write
(
"INCRBY
$name
$amount
\r\n
"
);
$this
->
write
(
"INCRBY
$name
$amount
\r\n
"
);
return
$this
->
get_response
();
}
function
&
decr
(
$name
,
$amount
=
1
)
{
public
function
decr
(
$name
,
$amount
=
1
)
{
$this
->
connect
();
if
(
$amount
==
1
)
$this
->
_
write
(
"DECR
$name
\r\n
"
);
$this
->
write
(
"DECR
$name
\r\n
"
);
else
$this
->
_
write
(
"DECRBY
$name
$amount
\r\n
"
);
$this
->
write
(
"DECRBY
$name
$amount
\r\n
"
);
return
$this
->
get_response
();
}
function
&
exists
(
$name
)
{
public
function
exists
(
$name
)
{
$this
->
connect
();
$this
->
_
write
(
"EXISTS
$name
\r\n
"
);
$this
->
write
(
"EXISTS
$name
\r\n
"
);
return
$this
->
get_response
();
}
function
&
delete
(
$name
)
{
public
function
delete
(
$name
)
{
$this
->
connect
();
$this
->
_
write
(
"DEL
$name
\r\n
"
);
$this
->
write
(
"DEL
$name
\r\n
"
);
return
$this
->
get_response
();
}
function
&
keys
(
$pattern
)
{
public
function
keys
(
$pattern
)
{
$this
->
connect
();
$this
->
_
write
(
"KEYS
$pattern
\r\n
"
);
return
explode
(
' '
,
$this
->
_
get_
valu
e
());
$this
->
write
(
"KEYS
$pattern
\r\n
"
);
return
explode
(
' '
,
$this
->
get_
respons
e
());
}
function
&
randomkey
()
{
public
function
randomkey
()
{
$this
->
connect
();
$this
->
_
write
(
"RANDOMKEY
\r\n
"
);
$this
->
write
(
"RANDOMKEY
\r\n
"
);
return
$this
->
get_response
();
}
function
&
rename
(
$src
,
$dst
,
$preserve
=
False
)
{
public
function
rename
(
$src
,
$dst
)
{
$this
->
connect
();
$this
->
write
(
"RENAME
$src
$dst
\r\n
"
);
return
$this
->
get_response
();
}
public
function
renamenx
(
$src
,
$dst
)
{
$this
->
connect
();
$this
->
_
write
(
$preserve
?
"RENAMENX
$src
$dst
\r\n
"
:
"RENAME
$src
$dst
\r\n
"
);
$this
->
write
(
"RENAME
NX
$src
$dst
\r\n
"
);
return
$this
->
get_response
();
}
function
&
expire
(
$name
,
$time
)
{
public
function
expire
(
$name
,
$time
)
{
$this
->
connect
();
$this
->
_
write
(
"EXPIRE
$name
$time
\r\n
"
);
$this
->
write
(
"EXPIRE
$name
$time
\r\n
"
);
return
$this
->
get_response
();
}
function
&
push
(
$name
,
$value
,
$tail
=
true
)
{
public
function
push
(
$name
,
$value
,
$tail
=
true
)
{
// default is to append the element to the list
$this
->
connect
();
$this
->
_
write
(
$this
->
write
(
(
$tail
?
'RPUSH'
:
'LPUSH'
)
.
"
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
return
$this
->
get_response
();
}
function
&
ltrim
(
$name
,
$start
,
$end
)
{
public
function
lpush
(
$name
,
$value
)
{
return
$this
->
push
(
$name
,
$value
,
false
);
}
public
function
rpush
(
$name
,
$value
)
{
return
$this
->
push
(
$name
,
$value
,
true
);
}
public
function
ltrim
(
$name
,
$start
,
$end
)
{
$this
->
connect
();
$this
->
_
write
(
"LTRIM
$name
$start
$end
\r\n
"
);
$this
->
write
(
"LTRIM
$name
$start
$end
\r\n
"
);
return
$this
->
get_response
();
}
function
&
lindex
(
$name
,
$index
)
{
public
function
lindex
(
$name
,
$index
)
{
$this
->
connect
();
$this
->
_
write
(
"LINDEX
$name
$index
\r\n
"
);
return
$this
->
_
get_
valu
e
();
$this
->
write
(
"LINDEX
$name
$index
\r\n
"
);
return
$this
->
get_
respons
e
();
}
function
&
pop
(
$name
,
$tail
=
true
)
{
public
function
pop
(
$name
,
$tail
=
true
)
{
$this
->
connect
();
$this
->
_
write
(
$this
->
write
(
(
$tail
?
'RPOP'
:
'LPOP'
)
.
"
$name
\r\n
"
);
return
$this
->
_get_value
();
return
$this
->
get_response
();
}
public
function
lpop
(
$name
,
$value
)
{
return
$this
->
pop
(
$name
,
$value
,
false
);
}
public
function
rpop
(
$name
,
$value
)
{
return
$this
->
pop
(
$name
,
$value
,
true
);
}
function
&
llen
(
$name
)
{
public
function
llen
(
$name
)
{
$this
->
connect
();
$this
->
_
write
(
"LLEN
$name
\r\n
"
);
$this
->
write
(
"LLEN
$name
\r\n
"
);
return
$this
->
get_response
();
}
function
&
lrange
(
$name
,
$start
,
$end
)
{
public
function
lrange
(
$name
,
$start
,
$end
)
{
$this
->
connect
();
$this
->
_
write
(
"LRANGE
$name
$start
$end
\r\n
"
);
$this
->
write
(
"LRANGE
$name
$start
$end
\r\n
"
);
return
$this
->
get_response
();
}
function
&
sort
(
$name
,
$query
=
false
)
{
public
function
sort
(
$name
,
$query
=
false
)
{
$this
->
connect
();
$this
->
_
write
(
$query
==
false
?
"SORT
$name
\r\n
"
:
"SORT
$name
$query
\r\n
"
);
$this
->
write
(
$query
==
false
?
"SORT
$name
\r\n
"
:
"SORT
$name
$query
\r\n
"
);
return
$this
->
get_response
();
}
function
&
lset
(
$name
,
$value
,
$index
)
{
public
function
lset
(
$name
,
$value
,
$index
)
{
$this
->
connect
();
$this
->
_
write
(
"LSET
$name
$index
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
$this
->
write
(
"LSET
$name
$index
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
return
$this
->
get_response
();
}
function
&
sadd
(
$name
,
$value
)
{
public
function
sadd
(
$name
,
$value
)
{
$this
->
connect
();
$this
->
_
write
(
"SADD
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
$this
->
write
(
"SADD
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
return
$this
->
get_response
();
}
function
&
srem
(
$name
,
$value
)
{
public
function
srem
(
$name
,
$value
)
{
$this
->
connect
();
$this
->
_
write
(
"SREM
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
$this
->
write
(
"SREM
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
return
$this
->
get_response
();
}
function
&
sismember
(
$name
,
$value
)
{
public
function
sismember
(
$name
,
$value
)
{
$this
->
connect
();
$this
->
_
write
(
"SISMEMBER
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
$this
->
write
(
"SISMEMBER
$name
"
.
strlen
(
$value
)
.
"
\r\n
$value
\r\n
"
);
return
$this
->
get_response
();
}
function
&
sinter
(
$sets
)
{
public
function
sinter
(
$sets
)
{
$this
->
connect
();
$this
->
_
write
(
'SINTER '
.
implode
(
' '
,
$sets
)
.
"
\r\n
"
);
$this
->
write
(
'SINTER '
.
implode
(
' '
,
$sets
)
.
"
\r\n
"
);
return
$this
->
get_response
();
}
function
&
smembers
(
$name
)
{
public
function
smembers
(
$name
)
{
$this
->
connect
();
$this
->
_
write
(
"SMEMBERS
$name
\r\n
"
);
$this
->
write
(
"SMEMBERS
$name
\r\n
"
);
return
$this
->
get_response
();
}
function
&
scard
(
$name
)
{
public
function
scard
(
$name
)
{
$this
->
connect
();
$this
->
_
write
(
"SCARD
$name
\r\n
"
);
$this
->
write
(
"SCARD
$name
\r\n
"
);
return
$this
->
get_response
();
}
function
&
select_db
(
$name
)
{
public
function
select_db
(
$name
)
{
$this
->
connect
();
$this
->
_
write
(
"SELECT
$name
\r\n
"
);
$this
->
write
(
"SELECT
$name
\r\n
"
);
return
$this
->
get_response
();
}
function
&
move
(
$name
,
$db
)
{
public
function
move
(
$name
,
$db
)
{
$this
->
connect
();
$this
->
_
write
(
"MOVE
$name
$db
\r\n
"
);
$this
->
write
(
"MOVE
$name
$db
\r\n
"
);
return
$this
->
get_response
();
}
function
&
save
(
$background
=
false
)
{
public
function
save
(
$background
=
false
)
{
$this
->
connect
();
$this
->
_
write
((
$background
?
"BGSAVE
\r\n
"
:
"SAVE
\r\n
"
));
$this
->
write
((
$background
?
"BGSAVE
\r\n
"
:
"SAVE
\r\n
"
));
return
$this
->
get_response
();
}
function
&
lastsave
()
{
public
function
bgsave
(
$background
=
false
)
{
return
$this
->
save
(
true
);
}
public
function
lastsave
()
{
$this
->
connect
();
$this
->
_
write
(
"LASTSAVE
\r\n
"
);
$this
->
write
(
"LASTSAVE
\r\n
"
);
return
$this
->
get_response
();
}
function
&
flush
(
$all
=
false
)
{
public
function
flush
db
(
$all
=
false
)
{
$this
->
connect
();
$this
->
_
write
(
$all
?
"FLUSH
\r\n
"
:
"FLUSHDB
\r\n
"
);
$this
->
write
(
$all
?
"FLUSH
ALL
\r\n
"
:
"FLUSHDB
\r\n
"
);
return
$this
->
get_response
();
}
public
function
flushall
()
{
return
$this
->
flush
(
true
);
}
function
&
info
()
{
public
function
info
()
{
$this
->
connect
();
$this
->
_
write
(
"INFO
\r\n
"
);
$this
->
write
(
"INFO
\r\n
"
);
$info
=
array
();
$data
=&
$this
->
get_response
();
foreach
(
explode
(
"
\r\n
"
,
$data
)
as
$l
)
{
...
...
@@ -260,7 +289,7 @@ class Redis {
return
$info
;
}
function
&
_
write
(
$s
)
{
private
function
write
(
$s
)
{
while
(
$s
)
{
$i
=
fwrite
(
$this
->
_sock
,
$s
);
if
(
$i
==
0
)
// || $i == strlen($s))
...
...
@@ -269,64 +298,68 @@ class Redis {
}
}
function
&
_
read
(
$len
=
1024
)
{
private
function
read
(
$len
=
1024
)
{
if
(
$s
=
fgets
(
$this
->
_sock
))
return
$s
;
$this
->
disconnect
();
trigger_error
(
"Cannot read from socket."
,
E_USER_ERROR
);
}
function
&
get_response
()
{
$data
=
trim
(
$this
->
_
read
());
private
function
get_response
()
{
$data
=
trim
(
$this
->
read
());
$c
=
$data
[
0
];
$data
=
substr
(
$data
,
1
);
switch
(
$c
)
{
case
'-'
:
trigger_error
(
substr
(
$data
,
0
,
4
)
==
'ERR '
?
substr
(
$data
,
4
)
:
$data
,
E_USER_ERROR
);
trigger_error
(
$data
,
E_USER_ERROR
);
break
;
case
'+'
:
return
$data
;
case
':'
:
$i
=
strpos
(
$data
,
'.'
)
!==
false
?
(
int
)
$data
:
(
float
)
$data
;
if
((
string
)
$i
!=
$data
)
trigger_error
(
"Cannot convert data '
$c$data
' to integer"
,
E_USER_ERROR
);
return
$i
;
case
'$'
:
return
$this
->
get_bulk_reply
(
$c
.
$data
);
case
'*'
:
$num
=
(
int
)
$data
;
if
((
string
)
$num
!=
$data
)
trigger_error
(
"Cannot convert multi-response header '
$data
' to integer"
,
E_USER_ERROR
);
$result
=
array
();
for
(
$i
=
0
;
$i
<
$num
;
$i
++
)
$result
[]
=&
$this
->
_
get_
valu
e
();
$result
[]
=&
$this
->
get_
respons
e
();
return
$result
;
default
:
return
$this
->
_get_value
(
$c
.
$data
);
trigger_error
(
"Invalid reply type byte: '
$c
'"
);
}
}
function
&
_
get_
value
(
$data
=
null
)
{
private
function
get_
bulk_reply
(
$data
=
null
)
{
if
(
$data
===
null
)
$data
=
&
trim
(
$this
->
_
read
());
$data
=
trim
(
$this
->
read
());
if
(
$data
==
'$-1'
)
return
null
;
$c
=
$data
[
0
];
$data
=
substr
(
$data
,
1
);
$i
=
strpos
(
$data
,
'.'
)
!==
false
?
(
int
)
$data
:
(
float
)
$data
;
if
((
string
)
$i
!=
$data
)
trigger_error
(
"Cannot convert data '
$c$data
' to integer"
,
E_USER_ERROR
);
if
(
$c
==
':'
)
return
$i
;
$bulklen
=
(
int
)
$data
;
if
((
string
)
$bulklen
!=
$data
)
trigger_error
(
"Cannot convert bulk read header '
$c$data
' to integer"
,
E_USER_ERROR
);
if
(
$c
!=
'$'
)
trigger_error
(
"Unkown response prefix for '
$c$data
'"
,
E_USER_ERROR
);
$buffer
=
''
;
while
(
true
)
{
$data
=
&
$this
->
_
read
(
);
$
i
-=
strlen
(
$data
);
while
(
$bulklen
)
{
$data
=
fread
(
$this
->
_
sock
,
$bulklen
);
$
bulklen
-=
strlen
(
$data
);
$buffer
.
=
$data
;
if
(
$i
<
0
)
break
;
}
return
substr
(
$buffer
,
0
,
-
2
);
$crlf
=
fread
(
$this
->
_sock
,
2
);
return
$buffer
;
}
}
}
//$r =& new Redis();
//var_dump($r->info());
$r
=
new
Redis
();
var_dump
(
$r
->
get
(
"foo"
));
var_dump
(
$r
->
info
());
?>
client-libraries/php/tests.php
View file @
8d03032a
...
...
@@ -7,7 +7,8 @@ require_once('redis.php');
$r
=&
new
Redis
(
'localhost'
);
$r
->
connect
();
$r
->
select_db
(
9
);
$r
->
flush
();
$r
->
flushdb
();
echo
"<pre>
\n
"
;
echo
$r
->
ping
()
.
"
\n
"
;
echo
$r
->
do_echo
(
'ECHO test'
)
.
"
\n
"
;
echo
"SET aaa "
.
$r
->
set
(
'aaa'
,
'bbb'
)
.
"
\n
"
;
...
...
@@ -29,15 +30,15 @@ echo 'SET a1 a2 a3' . $r->set('a1', 'a') . $r->set('a2', 'b') . $r->set('a3', 'c
echo
'KEYS a* '
.
print_r
(
$r
->
keys
(
'a*'
),
true
)
.
"
\n
"
;
echo
'RANDOMKEY '
.
$r
->
randomkey
(
'a*'
)
.
"
\n
"
;
echo
'RENAME a1 a0 '
.
$r
->
rename
(
'a1'
,
'a0'
)
.
"
\n
"
;
echo
'RENAMENX a0 a2 '
.
$r
->
rename
(
'a0'
,
'a2'
,
true
)
.
"
\n
"
;
echo
'RENAMENX a0 a1 '
.
$r
->
rename
(
'a0'
,
'a1'
,
true
)
.
"
\n
"
;
echo
'RENAMENX a0 a2 '
.
$r
->
rename
nx
(
'a0'
,
'a2'
)
.
"
\n
"
;
echo
'RENAMENX a0 a1 '
.
$r
->
rename
nx
(
'a0'
,
'a1'
)
.
"
\n
"
;
echo
'LPUSH a0 aaa '
.
$r
->
push
(
'a0'
,
'aaa'
)
.
"
\n
"
;
echo
'LPUSH a0 bbb '
.
$r
->
push
(
'a0'
,
'bbb'
)
.
"
\n
"
;
echo
'RPUSH a0 ccc '
.
$r
->
push
(
'a0'
,
'ccc'
,
false
)
.
"
\n
"
;
echo
'LLEN a0 '
.
$r
->
llen
(
'a0'
)
.
"
\n
"
;
echo
'LRANGE sdkjhfskdjfh 0 100 '
.
print_r
(
$r
->
lrange
(
'sdkjhfskdjfh'
,
0
,
100
),
true
)
.
"
\n
"
;
echo
'LRANGE a0 0 0 '
.
print_r
(
$r
->
lrange
(
'
sdkjhfskdjfh
'
,
0
,
0
),
true
)
.
"
\n
"
;
echo
'LRANGE a0 0 0 '
.
print_r
(
$r
->
lrange
(
'
a0
'
,
0
,
0
),
true
)
.
"
\n
"
;
echo
'LRANGE a0 0 100 '
.
print_r
(
$r
->
lrange
(
'a0'
,
0
,
100
),
true
)
.
"
\n
"
;
echo
'LTRIM a0 0 1 '
.
$r
->
ltrim
(
'a0'
,
0
,
1
)
.
"
\n
"
;
echo
'LRANGE a0 0 100 '
.
print_r
(
$r
->
lrange
(
'a0'
,
0
,
100
),
true
)
.
"
\n
"
;
...
...
@@ -82,5 +83,5 @@ echo 'BGSAVE ' . $r->save(true) . "\n";
echo
'LASTSAVE '
.
$r
->
lastsave
()
.
"
\n
"
;
echo
'INFO '
.
print_r
(
$r
->
info
())
.
"
\n
"
;
?>
\ No newline at end of file
echo
"</pre>
\n
"
;
?>
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