连接
mixed Memcache::pconnect(string $host[, int $port[, int $timeout]])
bool Memcache::connect(string $host[, int port[, int $timeout]])
bool Memcache::close(void)
说明:connect与pconnect的区别是connect可以被close关闭并且仅仅返回连接成功与否的布尔值,而pconnect不会在脚本执行结束后或close()被调用后关闭,返回Memcache对象,或者FALSE。持久化的连接仅仅会在Web服务器关机/重启时关闭。
服务器信息
参数说明:
- weight 为此服务器创建的桶的数量,用来控制此服务器被选中的权重,单个服务器被选中的概率是相对于所有服务器的总weight而言的
- status 控制此服务器是否可以被标记为在线状态
bool Memcache::addServer(
string $host[,
int $port=11211[,
bool $persistent=TRUE[,
int $weight[,
int $timeout=1[,
int $retry_interval=15[,
bool $status=TRUE[,
callback $failure_callback[,
int $timeoutms]]]]]]]])
增加一个服务器到连接池中。通过它打开的连接会在脚本执行结束后自动关闭,也可以使用Memcache::close()进行手动关闭。当使用这个方法的时候网络连接不会立刻建立,而是直到真正使用的时候才建立,因此在加入大量服务器到连接池时也是没有开销的,因为它们可能并不会被使用
bool Memcache::setServerParams(
string $host[,
int $port=11211[,
int $timeout[,
int $retry_interval=false[,
bool $status[,
callback $failure_callback]]]]])
int Memcache::getServerStatus(string $host[, int $port=11211])
返回服务器的在线/离线状态
string Memcache::getVersion(void)
获取服务器版本
array Memcache::getExtenedStats(
[string $type[,
int $slabid[,
int $limit=100]]])
获取缓存服务器池中所有服务器统计信息
array Memcache::getStats(
[string $type[,
int $slabid[,
int $limit=100]]])
获取服务器统计信息
bool Memcache::setCompressThreshold(int $threshold[, float $min_savings])
开启对于大值的自动压缩
- threshold:控制多大的值开始进行自动压缩
- min_saving:压缩率,0到1之间
bool memcache_debug(bool $on_off)
仅仅在PHP以--enable-debug方式编译时可以访问,总是返回TRUE
返回的服务器信息:
pid
服务器进程ID uptime 服务器已运行时间 time 服务器当前UNIX时间戳
version 服务器版本 rusage_user 进程的累计用户时间 rusage_system 进程累计的系统事件时间 curr_items 服务器当前已存储的项目数 total_items 从服务器开启到现在存储过的项目数 bytes 当前服务器使用空间,byte计算 curr_connections 当前打开的连接数 total_conections 自动服务器启动到现在打开的连接数 connection_structures 服务器分配的连接构造函数 cmd_get (获取)总请求次数 cmd_set (保存)总请求次数 get_misses 总未命中次数 get_hits 总命中次数 bytes_read 服务器接收的数据量
bytes_written 服务器向网络发送的数据量 limit_maxbytes 服务器允许使用的最大存储空间
数据处理
参数说明:
- key:将要分配给变量的key
- var:将要被存储的变量。字符串和整形以原文存储,其他类型序列化后存储
- flag:使用MEMCACHE_COMPRESSED标记对数据进行压缩(使用zlib)
- expire:当前写入缓存的数据的失效时间。设置为0表示数据永不过期,最大不能超过30天
bool Memcache::add(string $key, mixed $var[, int $flag[, int $expire]])
bool Memcache::set(string $key, mixed $var[, int $flag[, int $expire]])
string Memcache::get(string $key[, int &$flags])
array Memcache::get(array $keys[, array &$flags])
int Memcache::decrement(string $key[,int $value=1])
int Memcache::increment(string $key[,int $value=1])
bool Memcache::replace(string $key, mixed $var[, int $flag[, int $expire]])
bool Memcache::delete(string $key[, int $timeout=0])
bool Memcache::flush(void)
立即使所有已存在的元素失效,并不会真正释放任何资源,仅仅是使标记所有元素都失效,因此已经被使用的内存会被新的元素复写。