Callback Handler for failed connection
static
void
_failure
(string $host, int $port)
-
string
$host: Point to the host where memcached is listening for connections. This parameter may also specify other transports like unix:///path/to/memcached.sock to use UNIX domain sockets, in this case port must also be set to 0.
-
int
$port: Point to the port where memcached is listening for connections. This parameter is optional and its default value is 11211. Set this parameter to 0 when using UNIX domain sockets.
Constructor
Create Memcache Manager
<?php
$data = array(
'username'=>'memcachelover',
'email'=>'me@myemail.com',
'displayname'=>'Memcache Lover',
'location'=>array(
'country'=>'USA',
'state'=>'Oregon',
'city'=>'Portland'
)
);
$mc->add('memcachelover', $data, true, true, true); // adds the key with JSON encoding, encryption and compression
echo
$mc->get('memcachelover', false, false, true); // would echo the uncompressed, but still encrypted key
echo
$mc->get('memcachelover', false, true, true); // would echo the uncompressed, decrypted JSON formatted string
print_r($mc->get('memcachelover',true, true, true)); // would print the uncompressed, decrypted array
print_r($mc->statistics()); // this would print the array of usage stats for all servers
echo
$mc->report(); // this would print our custom report
$mc->flushmc(); // this would fluch all connected servers
$mc->close(); // this would close all connections
?>
MemcacheManager
__construct
()
Stores variable $var with $key ONLY if such key doesn't exist at the server yet.
<?php
$mc->add('mykey1', $myarray1, 30, true, true, true);
$mc->add('mykey2', $myarray2, 30, true, true);
$mc->add('mykey3', $myarray3, 30, true);
$mc->add('mykey4', $myarray4, 30);
$mc->add('mykey5', $myarray5);
?>
void
add
(string $key, string $var, [int $expire = 0], [bool $json = false], [bool $encrypt = false], [bool $zlib = false])
-
string
$key: The key that will be associated with the item
-
string
$var: The variable to store. Strings and integers are stored as is, other types are stored serialized.
-
int
$expire: Expiration time of the item. If it's equal to zero, the item will never expire. You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days).
-
bool
$json: Whether to encode using JSON
-
bool
$encrypt: Whether to encrypt string
-
bool
$zlib: Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
Add a memcached server to connection pool
void
addserver
(string $host, int $port)
-
string
$host: Point to the host where memcached is listening for connections. This parameter may also specify other transports like unix:///path/to/memcached.sock to use UNIX domain sockets, in this case port must also be set to 0.
-
int
$port: Point to the port where memcached is listening for connections. This parameter is optional and its default value is 11211. Set this parameter to 0 when using UNIX domain sockets.
Decrement item's value
void
decrement
(string $key, [int $value = 1])
-
string
$key: Key of the item do decrement.
-
int
$value: Decrement the item by value . Optional and defaults to 1.
Decrypt String with Key
The function decryptString()
string
decrypt
(string $string)
-
string
$string: String to Decrypt
Delete item from the server
void
delete
(string $key, [int $timeout = 0])
-
string
$key: The key associated with the item to delete.
-
int
$timeout: Execution time of the item. If it's equal to zero, the item will be deleted right away whereas if you set it to 30, the item will be deleted in 30 seconds.
Encrypt String with Key
The function encryptString()
string
encrypt
(string $string)
-
string
$string: String to Encrypt
Immediately invalidates all existing items but doesn't actually free any resources, it only marks all the items as expired, so occupied memory will be overwritten by new items.
Please note that after flushing, you have to wait about a second to be able to write to Memcached again. If you don't, your data is not saved.
void
flushmc
()
Retrieve item from the server
<?php
echo
$mc->get('mykey1', true, true, true);
echo
$mc->get('mykey2', true, true);
echo
$mc->get('mykey3', true);
?>
void
get
(string $keys, [bool $json = false], [ $decrypt = false], [bool $zlib = false], bool $encrypt)
-
string
$keys: The key or array of keys to fetch
-
bool
$json: Whether to decode using JSON
-
bool
$encrypt: Whether to decrypt string
-
bool
$zlib: Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
-
$decrypt
Increment item's value
void
increment
(string $key, [int $value = 1])
-
string
$key: Key of the item to increment
-
int
$value: Increment the item by value . Optional and defaults to 1.
PBKDF2 Implementation (as described in RFC 2898);
string
pbkdf2
(string $p, string $s, int $c, int $kl, [string $a = 'sha256'])
-
string
$p: Password
-
string
$s: Salt
-
int
$c: Iteration count (use 1000 or higher)
-
int
$kl: Derived key length
-
string
$a: Hash algorithm
Should be used to replace value of existing item with key
<?php
$mc->replace('mykey1', $myarray1, 30, true, true, true);
$mc->replace('mykey2', $myarray2, 30, true, true);
$mc->replace('mykey3', $myarray3, 30, true);
$mc->replace('mykey4', $myarray4, 30);
?>
void
replace
(string $key, string $var, [int $expire = 0], [bool $json = false], [bool $encrypt = false], [bool $zlib = false])
-
string
$key: The key that will be associated with the item
-
string
$var: The variable to store. Strings and integers are stored as is, other types are stored serialized.
-
int
$expire: Expiration time of the item. If it's equal to zero, the item will never expire. You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days).
-
bool
$json: Whether to encode using JSON
-
bool
$encrypt: Whether to encrypt string
-
bool
$zlib: Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
Get statistics from all servers in pool and generate custom report
string
report
()
Stores an item var with key on the memcached server
<?php
$mc->set('mykey1', $myarray1, 30, true, true, true);
$mc->set('mykey2', $myarray2, 30, true, true);
$mc->set('mykey3', $myarray3, 30, true);
$mc->set('mykey4', $myarray4, 30);
$mc->set('mykey5', $myarray5);
?>
void
set
(string $key, string $var, [int $expire = 0], [bool $json = false], [bool $encrypt = false], [bool $zlib = false])
-
string
$key: The key that will be associated with the item
-
string
$var: The variable to store. Strings and integers are stored as is, other types are stored serialized.
-
int
$expire: Expiration time of the item. If it's equal to zero, the item will never expire. You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days).
-
bool
$json: Whether to encode using JSON
-
bool
$encrypt: Whether to encrypt string
-
bool
$zlib: Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
Get statistics from all servers in pool
array
statistics
()
Returns a the servers online/offline status
<?php
if($mc->status()>
0) echo
'Connection Established';
?>
array
status
(string $host, int $port)
-
string
$host: Point to the host where memcached is listening for connections. This parameter may also specify other transports like unix:///path/to/memcached.sock to use UNIX domain sockets, in this case port must also be set to 0.
-
int
$port: Point to the port where memcached is listening for connections. This parameter is optional and its default value is 11211. Set this parameter to 0 when using UNIX domain sockets.