-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLiteMemcache.class.php
124 lines (109 loc) · 3.42 KB
/
LiteMemcache.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* LiteMemcache - tiny, simple and pure-PHP alternative to Memcache and Memcached clients
*
* Protocol specification @link https://github.com/memcached/memcached/blob/master/doc/protocol.txt
* GitHub repository @link https://github.com/ptrofimov/litememcache
* Contacts @author Petr Trofimov <[email protected]>
*/
class LiteMemcache
{
private $_socket, $_replies, $_lastReply;
public function __construct( $server )
{
$this->_socket = stream_socket_client( $server, $errno, $errstr );
if ( !$this->_socket )
{
throw new Exception( "$errstr ($errno)" );
}
$this->_replies = array(
'STORED' => true,
'NOT_STORED' => false,
'EXISTS' => false,
'OK' => true,
'ERROR' => false,
'DELETED' => true,
'NOT_FOUND' => false,
'ERROR' => null,
'CLIENT_ERROR' => null,
'SERVER_ERROR' => null );
}
public function add( $key, $value, $exptime = 0, $flags = 0 )
{
return $this->query( array( "add $key $flags $exptime " . strlen( $value ), $value ) );
}
public function append( $key, $value )
{
return $this->query( array( "append $key 0 0 " . strlen( $value ), $value ) );
}
public function cas( $key, $value, $cas, $exptime = 0, $flags = 0 )
{
return $this->query( array( "cas $key $flags $exptime " . strlen( $value ) . " $cas", $value ) );
}
public function decr( $key, $value = 1 )
{
return $this->query( "decr $key $value" );
}
public function del( $key )
{
return $this->query( "delete $key" );
}
public function flushAll( $exptime = 0 )
{
return $this->query( "flush_all $exptime" );
}
public function get( $key, $ext = false )
{
$keys = array_fill_keys( ( array ) $key,
$ext ? array( 'value' => null, 'flags' => null, 'cas' => null ) : null );
$words = $this->query( ( $ext ? 'gets' : 'get' ) . ' ' . implode( ' ', array_keys( $keys ) ) );
while ( $words[ 0 ] == 'VALUE' )
{
$value = fread( $this->_socket, $words[ 3 ] + 2 );
$keys[ $words[ 1 ] ] = $ext ? array(
'value' => substr( $value, 0, strlen( $value ) - 2 ),
'flags' => $words[ 2 ],
'cas' => $words[ 4 ] ) : substr( $value, 0, strlen( $value ) - 2 );
$words = $this->_readLine();
}
return is_array( $key ) ? $keys : reset( $keys );
}
public function getLastReply()
{
return $this->_lastReply;
}
public function incr( $key, $value = 1 )
{
return $this->query( "incr $key $value" );
}
public function prepend( $key, $value )
{
return $this->query( array( "prepend $key 0 0 " . strlen( $value ), $value ) );
}
public function query( $query )
{
$query = is_array( $query ) ? implode( "\r\n", $query ) : $query;
fwrite( $this->_socket, $query . "\r\n" );
return $this->_readLine();
}
public function replace( $key, $value, $exptime = 0, $flags = 0 )
{
return $this->query( array( "replace $key $flags $exptime " . strlen( $value ), $value ) );
}
public function set( $key, $value, $exptime = 0, $flags = 0 )
{
return $this->query( array( "set $key $flags $exptime " . strlen( $value ), $value ) );
}
private function _readLine()
{
$line = fgets( $this->_socket );
$this->_lastReply = substr( $line, 0, strlen( $line ) - 2 );
$words = explode( ' ', $this->_lastReply );
$result = isset( $this->_replies[ $words[ 0 ] ] ) ? $this->_replies[ $words[ 0 ] ] : $words;
if ( is_null( $result ) )
{
throw new Exception( $this->_lastReply );
}
return ( is_array( $result ) && count( $result ) == 1 ) ? reset( $result ) : $result;
}
}