PHP共享内存yac操作类

http://www.laruence.com/2013/03/18/2846.html   鸟哥介绍

https://www.cnblogs.com/willamwang/p/8918377.html  扩展安装

复制代码
<?php

/**
 * 进程间共享内存操作类
 */
class Pshmop
{
    protected static $_models = array();
    private $_yac = null;
    private static $_keyPrefix = 'shm_';
    private static $_ttlMaxTime = 7776000;  //86400*90 为防止永久贮存及保存时间过久造成内存消耗严重导致数据被踢出

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Order the static model class
     */
    public static function model($className = __CLASS__)
    {
        $model = null;
        if (isset(self::$_models[$className]))
            $model = self::$_models[$className];
        else {
            $model = self::$_models[$className] = new $className(null);
        }
        return $model;
    }

   public function __construct() {
        if(extension_loaded("yac")){
            $this->_yac = new Yac(self::$_keyPrefix);
        }
    }

    /** 
    * add value
    * @param mixed $keys
    * @param mixed $value
    * @param int $ttl
    * @return mixed
    */
    public function add($key, $value, $ttl=-1){
        if(empty($key)){
            return null;
        }

        if(empty($this->_yac)){
            return null;
        }

        if($ttl<0 || $ttl>self::$_ttlMaxTime){
            $ttl = self::$_ttlMaxTime;
        }

        return $this->_yac->add($key, $value, $ttl);
    }

    /** 
    * set value
    * @param mixed $keys
    * @param mixed $value
    * @param int $ttl
    * @return mixed
    */
    public function set($key, $value, $ttl=-1){
        if(empty($key)){
            return null;
        }

        if(empty($this->_yac)){
            return null;
        }

        if($ttl<0 || $ttl>self::$_ttlMaxTime){
            $ttl = self::$_ttlMaxTime;
        }

        return $this->_yac->set($key, $value, $ttl);
    }

    /** 
    * get value
    * @param mixed $keys
    * @return mixed
    */
    public function get($key){
        if(empty($key)){
            return null;
        }

        if(empty($this->_yac)){
            return null;
        }

        return $this->_yac->get($key);
    }

    /** 
    * delete key
    * @param mixed $keys
    * @param int $delay
    * @return mixed
    */
    public function delete($key, $delay=0){
        if(empty($key)){
            return null;
        }

        if(empty($this->_yac)){
            return null;
        }

        return $this->_yac->delete($key, $delay);
    }

    /**
    * flush shm
    * @param void
    * @return mixed
    */
    public function flush(){

        if(empty($this->_yac)){
            return null;
        }

        return $this->_yac->flush();
    }

    /**
    * get shm info
    * @param void
    * @return mixed
    */
    public function info(){

        if(empty($this->_yac)){
            return null;
        }

        return $this->_yac->info();
    }

}
复制代码

 

posted @   温柔的风  阅读(875)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示