PHP网页设计MemCached的PHP客户端操作类一
通过这段时间的学习实践,对软件开发有了更多新的认识,不在局限于之前的片面性。当然,现在所学到的东西其实并不多,离当一个真正的程序员,还有很大的差距。cache|客户端 MemCached的PHP客户端操作类一<?php
//
// +---------------------------------------------------------------------------+
// | memcached client, PHP |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net> |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | 1. Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | 2. Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
// | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
// | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
// | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT|
// | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF|
// | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// +---------------------------------------------------------------------------+
// | Author: Ryan T. Dean <rtdean@cytherianage.net> |
// | Heavily influenced by the Perl memcached client by Brad Fitzpatrick. |
// | Permission granted by Brad Fitzpatrick for relicense of ported Perl |
// | client logic under 2-clause BSD license. |
// +---------------------------------------------------------------------------+
//
// $TCAnet$
//
/**
* This is the PHP client for memcached - a distributed memory cache daemon.
* More information is available at http://www.danga.com/memcached/
*
* Usage example:
*
* require_once 'memcached.php';
*
* $mc = new memcached(array(
* 'servers' => array('127.0.0.1:10000',
* array('192.0.0.1:10010', 2),
* '127.0.0.1:10020'),
* 'debug' => false,
* 'compress_threshold' => 10240,
* 'persistant' => true));
*
* $mc->add('key', array('some', 'array'));
* $mc->replace('key', 'some random string');
* $val = $mc->get('key');
*
* @authorRyan T. Dean <rtdean@cytherianage.net>
* @package memcached-client
* @version 0.1.2
*/
// {{{ requirements
// }}}
// {{{ constants
// {{{ flags
/**
* Flag: indicates data is serialized
*/
define("MEMCACHE_SERIALIZED", 1<<0);
/**
* Flag: indicates data is compressed
*/
define("MEMCACHE_COMPRESSED", 1<<1);
// }}}
/**
* Minimum savings to store data compressed
*/
define("COMPRESSION_SAVINGS", 0.20);
// }}}
// {{{ class memcached
/**
* memcached client class implemented using (p)fsockopen()
*
* @authorRyan T. Dean <rtdean@cytherianage.net>
* @package memcached-client
*/
class memcached
{
// {{{ properties
// {{{ public
/**
* Command statistics
*
* @var array
* @accesspublic
*/
var $stats;
// }}}
// {{{ private
/**
* Cached Sockets that are connected
*
* @var array
* @accessprivate
*/
var $_cache_sock;
/**
* Current debug status; 0 - none to 9 - profiling
*
* @var boolean
* @accessprivate
*/
var $_debug;
/**
* Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
*
* @var array
* @accessprivate
*/
var $_host_dead;
/**
* Is compression available?
*
* @var boolean
* @accessprivate
*/
var $_have_zlib;
/**
* Do we want to use compression?
*
* @var boolean
* @accessprivate
*/
var $_compress_enable;
/**
* At how many bytes should we compress?
*
* @var interger
* @accessprivate
*/
var $_compress_threshold;
/**
* Are we using persistant links?
*
* @var boolean
* @accessprivate
*/
var $_persistant;
/**
* If only using one server; contains ip:port to connect to
*
* @var string
* @accessprivate
*/
var $_single_sock;
/**
* Array containing ip:port or array(ip:port, weight)
*
* @var array
* @accessprivate
*/
var $_servers;
/**
* Our bit buckets
*
* @var array
* @accessprivate
*/
var $_buckets;
/**
* Total # of bit buckets we have
*
* @var interger
* @accessprivate
*/
var $_bucketcount;
/**
* # of total servers we have
*
* @var interger
* @accessprivate
*/
var $_active;
// }}}
// }}}
// {{{ methods
// {{{ public functions
// {{{ memcached()
/**
* Memcache initializer
*
* @param array $args Associative array of settings
*
* @returnmixed
* @accesspublic
*/
function memcached ($args)
{
$this->set_servers($args['servers']);
$this->_debug = $args['debug'];
$this->stats = array();
$this->_compress_threshold = $args['compress_threshold'];
$this->_persistant = isset($args['persistant']) ? $args['persistant'] : false;
$this->_compress_enable = true;
$this->_have_zlib = function_exists("gzcompress");
$this->_cache_sock = array();
$this->_host_dead = array();
}
// }}}
// {{{ add()
/**
* Adds a key/value to the memcache server if one isn't already set with
* that key
*
* @param string $key Key to set with data
* @param mixed $val Value to store
* @param interger $exp (optional) Time to expire data at
*
* @returnboolean
* @accesspublic
*/
function add ($key, $val, $exp = 0)
{
return $this->_set('add', $key, $val, $exp);
}
// }}}
// {{{ decr()
/**
* Decriment a value stored on the memcache server
*
* @param string $key Key to decriment
* @param interger $amt (optional) Amount to decriment
*
* @returnmixed FALSE on failure, value on success
* @accesspublic
*/
function decr ($key, $amt=1)
{
return $this->_incrdecr('decr', $key, $amt);
}
// }}}
// {{{ delete()
/**
* Deletes a key from the server, optionally after $time
*
* @param string $key Key to delete
* @param interger $time (optional) How long to wait before deleting
*
* @returnbooleanTRUE on success, FALSE on failure
* @accesspublic
*/
function delete ($key, $time = 0)
{
if (!$this->_active)
return false;
$sock = $this->get_sock($key);
if (!is_resource($sock))
return false;
$key = is_array($key) ? $key : $key;
$this->stats['delete']++;
$cmd = "delete $key $time\r\n";
if(!fwrite($sock, $cmd, strlen($cmd)))
{
$this->_dead_sock($sock);
return false;
}
$res = trim(fgets($sock));
if ($this->_debug)
printf("MemCache: delete %s (%s)\n", $key, $res);
if ($res == "DELETED")
return true;
return false;
}
// }}}
// {{{ disconnect_all()
/**
* Disconnects all connected sockets
*
* @accesspublic
*/
function disconnect_all ()
{
foreach ($this->_cache_sock as $sock)
fclose($sock);
$this->_cache_sock = array();
}
// }}}
// {{{ enable_compress()
/**
* Enable / Disable compression
*
* @param boolean$enableTRUE to enable, FALSE to disable
*
* @accesspublic
*/
function enable_compress ($enable)
{
$this->_compress_enable = $enable;
}
// }}}
// {{{ forget_dead_hosts()
/**
* Forget about all of the dead hosts
*
* @accesspublic
*/
function forget_dead_hosts ()
{
$this->_host_dead = array();
}
// }}}
// {{{ get()
/**
* Retrieves the value associated with the key from the memcache server
*
* @paramstring $key Key to retrieve
*
* @returnmixed
* @accesspublic
*/
function get ($key)
{
if (!$this->_active)
return false;
$sock = $this->get_sock($key);
if (!is_resource($sock))
return false;
$this->stats['get']++;
$cmd = "get $key\r\n";
if (!fwrite($sock, $cmd, strlen($cmd)))
{
$this->_dead_sock($sock);
return false;
}
$val = array();
$this->_load_items($sock, $val);
if ($this->_debug)
foreach ($val as $k => $v)
printf("MemCache: sock %s got %s => %s\r\n", $sock, $k, $v);
return $val[$key];
}
// }}}
// {{{ get_multi()
/**
* Get multiple keys from the server(s)
*
* @param array $keys Keys to retrieve
*
* @returnarray
* @accesspublic
*/
function get_multi ($keys)
{
if (!$this->_active)
return false;
$this->stats['get_multi']++;
foreach ($keys as $key)
{
$sock = $this->get_sock($key);
if (!is_resource($sock)) continue;
$key = is_array($key) ? $key : $key;
if (!isset($sock_keys[$sock]))
{
$sock_keys[$sock] = array();
$socks[] = $sock;
}
$sock_keys[$sock][] = $key;
}
// Send out the requests
foreach ($socks as $sock)
{
$cmd = "get";
foreach ($sock_keys[$sock] as $key)
{
$cmd .= " ". $key;
}
$cmd .= "\r\n";
if (fwrite($sock, $cmd, strlen($cmd)))
{
$gather[] = $sock;
} else
{
$this->_dead_sock($sock);
}
}
// Parse responses
$val = array();
foreach ($gather as $sock)
{
$this->_load_items($sock, $val);
}
if ($this->_debug)
foreach ($val as $k => $v)
printf("MemCache: got %s => %s\r\n", $k, $v);
return $val;
}
// }}}
// {{{ incr()
/**
* Increments $key (optionally) by $amt
*
* @param string $key Key to increment
* @param interger $amt (optional) amount to increment
*
* @returninterger New key value?
* @accesspublic
*/
function incr ($key, $amt=1)
{
return $this->_incrdecr('incr', $key, $amt);
}
// }}}
// {{{ replace()
/**
* Overwrites an existing value for key; only works if key is already set
*
* @param string $key Key to set value as
* @param mixed $value Value to store
* @param interger $exp (optional) Experiation time
*
* @returnboolean
* @accesspublic
*/
function replace ($key, $value, $exp=0)
{
return $this->_set('replace', $key, $value, $exp);
}
// }}}
// {{{ run_command()
/**
* Passes through $cmd to the memcache server connected by $sock; returns
* output as an array (null array if no output)
*
* NOTE: due to a possible bug in how PHP reads while using fgets(), each
* line may not be terminated by a \r\n.More specifically, my testing
* has shown that, on FreeBSD at least, each line is terminated only
* with a \n.This is with the PHP flag auto_detect_line_endings set
* to falase (the default).
*
* @param resource $sock Socket to send command on
* @param string $cmd Command to run
*
* @returnarray Output array
* @accesspublic
*/
function run_command ($sock, $cmd)
{
if (!is_resource($sock))
return array();
if (!fwrite($sock, $cmd, strlen($cmd)))
return array();
while (true)
{
$res = fgets($sock);
$ret[] = $res;
if (preg_match('/^END/', $res))
break;
if (strlen($res) == 0)
break;
}
return $ret;
}
// }}}
// {{{ set()
/**
* Unconditionally sets a key to a given value in the memcache.Returns true
* if set successfully.
*
* @param string $key Key to set value as
* @param mixed $value Value to set
* @param interger $exp (optional) Experiation time
*
* @returnbooleanTRUE on success
* @accesspublic
*/
function set ($key, $value, $exp=0)
{
return $this->_set('set', $key, $value, $exp);
}
// }}}
// {{{ set_compress_threshold()
/**
* Sets the compression threshold
*
* @param interger $threshThreshold to compress if larger than
*
* @accesspublic
*/
function set_compress_threshold ($thresh)
{
$this->_compress_threshold = $thresh;
}
// }}}
// {{{ set_debug()
/**
* Sets the debug flag
*
* @param boolean$dbg TRUE for debugging, FALSE otherwise
*
* @accesspublic
*
* @see memcahced::memcached
*/
function set_debug ($dbg)
{
$this->_debug = $dbg;
}
// }}}
// {{{ set_servers()
/**
* Sets the server list to distribute key gets and puts between
*
* @param array $list Array of servers to connect to
*
* @accesspublic
*
* @see memcached::memcached()
*/
function set_servers ($list)
{
$this->_servers = $list;
$this->_active = count($list);
$this->_buckets = null;
$this->_bucketcount = 0;
$this->_single_sock = null;
if ($this->_active == 1)
$this->_single_sock = $this->_servers;
}
// }}}
// }}}
// {{{ private methods
// {{{ _close_sock()
/**
* Close the specified socket
*
* @param string $sock Socket to close
*
* @accessprivate
*/
function _close_sock ($sock)
{
$host = array_search($sock, $this->_cache_sock);
fclose($this->_cache_sock[$host]);
unset($this->_cache_sock[$host]);
}
// }}}
// {{{ _connect_sock()
/**
* Connects $sock to $host, timing out after $timeout
*
* @param interger $sock Socket to connect
* @param string $host Host:IP to connect to
* @param float $timeout (optional) Timeout value, defaults to 0.25s
*
* @returnboolean
* @accessprivate
*/
function _connect_sock (&$sock, $host, $timeout = 0.25)
{
list ($ip, $port) = explode(":", $host);
if ($this->_persistant == 1)
{
$sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);
} else
{
$sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
}
if (!$sock)
return false;
return true;
}
// }}}
// {{{ _dead_sock()
/**
* Marks a host as dead until 30-40 seconds in the future
*
* @param string $sock Socket to mark as dead
*
* @accessprivate
*/
function _dead_sock ($sock)
{
$host = array_search($sock, $this->_cache_sock);
list ($ip, $port) = explode(":", $host);
$this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));
$this->_host_dead[$host] = $this->_host_dead[$ip];
unset($this->_cache_sock[$host]);
}
// }}}
// {{{ get_sock()
/**
* get_sock
*
* @param string $key Key to retrieve value for;
*
* @returnmixed resource on success, false on failure
* @accessprivate
*/
function get_sock ($key)
{
if (!$this->_active)
return false;
if ($this->_single_sock !== null)
return $this->sock_to_host($this->_single_sock);
$hv = is_array($key) ? intval($key) : $this->_hashfunc($key);
if ($this->_buckets === null)
{
foreach ($this->_servers as $v)
{
if (is_array($v))
{
for ($i=0; $i<$v; $i++)
$bu[] = $v;
} else
{
$bu[] = $v;
}
}
$this->_buckets = $bu;
$this->_bucketcount = count($bu);
}
$realkey = is_array($key) ? $key : $key;
for ($tries = 0; $tries<20; $tries++)
{
$host = $this->_buckets[$hv % $this->_bucketcount];
$sock = $this->sock_to_host($host);
if (is_resource($sock))
return $sock;
$hv += $this->_hashfunc($tries . $realkey);
}
return false;
}
// }}}
// {{{ _hashfunc()
/**
* Creates a hash interger based on the $key
*
* @param string $key Key to hash
*
* @returninterger Hash value
* @accessprivate
*/
function _hashfunc ($key)
{
$hash = 0;
for ($i=0; $i<strlen($key); $i++)
{
$hash = $hash*33 + ord($key[$i]);
}
return $hash;
}
// }}}
// {{{ _incrdecr()
/**
* Perform increment/decriment on $key
*
* @param string $cmd Command to perform
* @param string $key Key to perform it on
* @param interger $amt Amount to adjust
*
* @returninterger New value of $key
* @accessprivate
*/
function _incrdecr ($cmd, $key, $amt=1)
{
if (!$this->_active)
return null;
$sock = $this->get_sock($key);
if (!is_resource($sock))
return null;
$key = is_array($key) ? $key : $key;
$this->stats[$cmd]++;
if (!fwrite($sock, "$cmd $key $amt\r\n"))
return $this->_dead_sock($sock);
stream_set_timeout($sock, 1, 0);
$line = fgets($sock);
if (!preg_match('/^(\d+)/', $line, $match))
return null;
return $match;
}
// }}}
// {{{ _load_items()
/**
* Load items into $ret from $sock
*
* @param resource $sock Socket to read from
* @param array $ret Returned values
*
* @accessprivate
*/
function _load_items ($sock, &$ret)
{
while (1)
{
$decl = fgets($sock);
if ($decl == "END\r\n")
{
return true;
} elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))
{
list($rkey, $flags, $len) = array($match, $match, $match);
$bneed = $len+2;
$offset = 0;
while ($bneed > 0)
{
$data = fread($sock, $bneed);
$n = strlen($data);
if ($n == 0)
break;
$offset += $n;
$bneed -= $n;
$ret[$rkey] .= $data;
}
if ($offset != $len+2)
{
// Something is borked!
if ($this->_debug)
printf("Something is borked!key %s expecting %d got %d length\n", $rkey, $len+2, $offset);
unset($ret[$rkey]);
$this->_close_sock($sock);
return false;
}
$ret[$rkey] = rtrim($ret[$rkey]);
if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)
$ret[$rkey] = gzuncompress($ret[$rkey]);
if ($flags & MEMCACHE_SERIALIZED)
$ret[$rkey] = unserialize($ret[$rkey]);
} else
{
if ($this->_debug)
print("Error parsing memcached response\n");
return 0;
}
}
}
// }}}
// {{{ _set()
/**
* Performs the requested storage operation to the memcache server
*
* @param string $cmd Command to perform
* @param string $key Key to act on
* @param mixed $val What we need to store
* @param interger $exp When it should expire
*
* @returnboolean
* @accessprivate
*/
function _set ($cmd, $key, $val, $exp)
{
if (!$this->_active)
return false;
$sock = $this->get_sock($key);
if (!is_resource($sock))
return false;
$this->stats[$cmd]++;
$flags = 0;
if (!is_scalar($val))
{
$val = serialize($val);
$flags |= MEMCACHE_SERIALIZED;
if ($this->_debug)
printf("client: serializing data as it is not scalar\n");
}
$len = strlen($val);
if ($this->_have_zlib && $this->_compress_enable &&
$this->_compress_threshold && $len >= $this->_compress_threshold)
{
$c_val = gzcompress($val, 9);
$c_len = strlen($c_val);
if ($c_len < $len*(1 - COMPRESS_SAVINGS))
{
if ($this->_debug)
printf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len);
$val = $c_val;
$len = $c_len;
$flags |= MEMCACHE_COMPRESSED;
}
}
if (!fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
return $this->_dead_sock($sock);
$line = trim(fgets($sock));
if ($this->_debug)
{
if ($flags & MEMCACHE_COMPRESSED)
$val = 'compressed data';
printf("MemCache: %s %s => %s (%s)\n", $cmd, $key, $val, $line);
}
if ($line == "STORED")
return true;
return false;
}
// }}}
// {{{ sock_to_host()
, /**
* Returns the socket for the host
*
* @param string $host Host:IP to get socket for
*
* @returnmixed IO Stream or false
* @accessprivate
*/
function sock_to_host ($host)
{
if (isset($this->_cache_sock[$host]))
return $this->_cache_sock[$host];
$now = time();
list ($ip, $port) = explode (":", $host);
if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
return null;
if (!$this->_connect_sock($sock, $host))
return $this->_dead_sock($host);
// Do not buffer writes
stream_set_write_buffer($sock, 0);
$this->_cache_sock[$host] = $sock;
return $this->_cache_sock[$host];
}
// }}}
// }}}
// }}}
}
// }}}
?>
你的留言本应该加入注册以及分页功能了,而如果你更强的话,UI(用户界面)也可以加强,完成之后,感觉是不是特有成就感?不管怎么样,咱好歹是写了一个动态网站程序了,放在自己的网站上耍耍吧。 在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、 我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。 写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。 刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。 首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。 在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。 学好程序语言,多些才是王道,写两个小时代码的作用绝对超过看一天书,这个我是深有体会(顺便还能练打字速度)。 装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。 这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。 说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年具体的记不清啦,囧。 没接触过框架的人,也不用害怕,其实框架就是一种命名规范及插件,学会一个框架其余的框架都很好上手的。 我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。 基础有没有对学习php没有太大区别,关键是兴趣。 个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。 其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎 你很难利用原理去编写自己的代码。对于php来说,系统的学习我认为还是很重要的,当你有一定理解后,你可你针对某种效果研究,我想那时你不会只是复制代码的水平了。 小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。 Ps:以上纯属原创,如有雷同,纯属巧合 最后介绍一个代码出错,但是老找不到错误方法,就是 go to wc (囧),出去换换气没准回来就找到错误啦。
页:
[1]