1.redis
Redis is fully open source free, complies with BSD protocol, advanced Key-value persistent products. It is commonly referred to as a data structure server, because the value can be a string (string), a hash (MAP), a list, a collection (sets), and an ordered set (sorted sets), and other types
Installing Redis
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz$ tar xzf redis-2.8.17.tar. gz$ cd Redis-2.8.17$ Make
Using Redis with PHP
<?PHP/*get the database name from the platform*/$dbname= ""; /*taking host,port,user,pwd from environment variables*/$host= ' ';$port= ' ';$user= ' ';$pwd= ' '; Try { /*After the connection is established, auth validation is required before the collection operation*/ $redis=NewRedis (); $ret=$redis->connect ($host,$port); if($ret===false) { die($redis-GetLastError ()); } $ret=$redis->auth ($user. "-" .$pwd. "-" .$dbname); if($ret===false) { die($redis-GetLastError ()); } /*Next, you can operate the library, please refer to the Phpredis official documentation for details.*/ $redis-flushdb (); $ret=$redis->set ("Key", "value"); if($ret===false) { die($redis-GetLastError ()); } Else { Echo"OK".$redis->get ("Key"); } } Catch(redisexception$e) { die("Uncaught exception".$e-getMessage ());}?>
2.membercached
Installing the Linux membercached
PHP using membercached
<?PHP//creating an instance of a Mem object $mem=NewMemcache; if(!$mem->connect ("10.18.110.213", 11211)){ die(' Connection failed! ')); } //add a//1. Add a String/*if ($mem->set (' Key1 ', "Beijing", memcache_compressed,60)) {echo ' Add OK '; }*/ //2. Add a value/*if ($mem->set (' Key1 ', 100,memcache_compressed,60)) {echo ' Add OK '; }*/ //3. Add an array//In the Add array is, as needed. Hope the serial number is put in,//serialize<=>unserialize, if necessary, can also json_encode <=> json_decode $arr=Array("BJ", ' TJ '); if($mem->set (' Key1 ',$arr, memcache_compressed, Time() +31*3600*24)){ Echo' Add array ok99111 '; } //4. Adding Objects/*class dog{public $name; Public $age; Public function __construct ($name, $age) {$this->name= $name; $this->age= $age; }} $dog 1=new dog (' Puppy ', 50); if ($mem->set (' Key1 ', $dog 1,memcache_compressed,60)) {echo ' Add object OK '; }*/ //5. Adding a null Boolean value/*if ($mem->set (' Key1 ', false,memcache_compressed,60)) {echo ' adds Boolean ok '; }*/ //6. Type the resource into./*$con =mysql_connect ("127.0.0.1", "root", "root"); if (! $con) {die (' failed to connect to database '); } var_dump ($con); echo "<br/>"; if ($mem->set (' Key1 ', $con, memcache_compressed,60)) {echo ' Add resource ok '; }*/ //Enquiry $val=$mem->get (' Key1 ');//Modify//can use replace if($mem->replace ("Key11", ' Hello ', memcache_compressed,60)){ Echo' Replace OK '; }Else{ Echo' Replace no OK '; } //Delete Echo"<br/>"; if($mem->delete (' Key14 ')){ Echo' Key14 Delete '; }Else{ Echo' KEY14 does not exist '; }
Accelerated access to Redis and membercached