php memcached tool

本文介绍了一个名为memcachedTool的PHP工具,它提供了一种统一的方法来操作memcached缓存,无论使用的是php_memcache还是php_memcached扩展。在使用前需要安装相应的PHP扩展,并通过几步简单步骤即可开始使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

memcachedTool

介绍

基于interface 实现php不管是php_memcache扩展还是php_memcached扩展, 采用统一的方法来操作memcached缓存

前提

安装php_memcache或者php_memcached扩展确保能用

使用教程

第一步: 下载源码
点击去github下载
点击去gitee下载

第二步: 导入工具类

// 导入工具类
<?php
use extend\cache\memcache\MemcacheTool;
use extend\cache\memcache\MemcachedTool;

include 'extend/cache/memcache/MemcacheInterface.php';
include 'extend/cache/memcache/Memcache.php';
include 'extend/cache/memcache/Memcached.php';

第三步: 使用

// 实例化
// $memcache = new MemcacheTool();
$memcache = new MemcachedTool();

// 设置
$memcache->set('name','phpxxo');

// 获取
$name = $memcache->get('name');

附: MemcacheInterface.php (完整代码在上面附件下载, 没有积分的可在上面的gitee 或github中去下载)

<?php
/**
 * php_memcache, php_memcached interface
 * Author:phpxxo
 * Email:244013508@qq.com
 * Create Date:2019-10-4
 */
namespace extend\cache\memcache;
interface MemcacheInterface{

    /**
     * 初始化, 连接memcached
     */
    function __construct(Array $options=[]);

    /**
     * 关闭memcached连接
     */
    function __destruct();

    /**
     * 设置缓存
     * @param    string  $name 缓存变量名
     * @param    string  $value 缓存值
     * @param    int  $expire 有效期 秒
     * @param    boolean  $hasPrefix 是否有前缀
     * @return   boolean
     */
     function set($name,$value,$expire=0,$hasPrefix=true);

    /**
     * 获取缓存值
     * @param    string  $name 缓存变量名
     * @param    string  $default 默认值
     * @param    boolean  $hasPrefix 是否有前缀
     */
     function get($name,$default='',$hasPrefix=true);

    /**
     * 自增缓存(针对数值)
     * @param    string  $name 缓存变量名
     * @param    int  $step 步长
     * @param    boolean  $hasPrefix 是否有前缀
     */
     function incr($name,$step=1,$hasPrefix=true);

    /**
     * 自减缓存(针对数值)
     * @param    string  $name 缓存变量名
     * @param    int  $step 步长
     * @param    boolean  $hasPrefix 是否有前缀
     */
     function decr($name,$step=1,$hasPrefix=true);

    /**
     * 删除缓存
     * @param    string  $name 缓存变量名
     * @param    boolean  $clearMemory 是否释放内存空间
     * @param    boolean  $clearMemory 是否有前缀
     */
     function del($name,$clearMemory=false,$hasPrefix=true);

    /**
     * 刷新所有缓存过期
     * @param boolean $clearMemory 是否释放内存空间
     * @return boolean
     */
     function flush($clearMemory=false);

    /**
     * 获取带前缀的缓存存储key
     * @param    string  $name 缓存变量名
     * @return string
     */
     function getCacheKey($name);

    /**
     * 获取所有key
     * @return array
     */
     function getAllkeys();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值