有没有办法将 php 对象保留在内存中以避免磁盘读取和写入?

发布于 2025-01-01 04:13:18 字数 131 浏览 2 评论 0原文

所以我有一个从磁盘 gnugpg 读取文件的对象,它似乎总是在主目录中创建一个 gnugpg 密钥环。

我想避免每次从 apache 调用 php 脚本时都必须加载这个对象。

有没有办法让 php 对象保留在内存中?

So I have an object that reads a file from disk gnugpg it appears to always create a gnugpg key ring in a home directory.

I want to avoid having to load this object every time a php script is called from apache.

is there away to have a php object stay in memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-01-08 04:13:18

如果它是一个不占用太多内存并且可序列化的小对象,您可以将其存储在会话中:

function    getSessionObject($objectName, $params){

    $sessionObjectSerialized = getSessionVariable($objectName, FALSE);

    if($sessionObjectSerialized == FALSE){
        $sessionObjectSerialized = constructSessionObject($objectName, $params);
        setSessionVariable($objectName, $sessionObjectSerialized);
    }

    $sessionObject = unserialize($sessionObjectSerialized);

    return $sessionObject;
}


function    constructSessionObject($objectName, $params = array()){

    switch($objectName){

        case('gnugpg_key_ring'):{
            $gnugpgKeyRing = getGNUPGKeyRing(); //do whatever you need to do to make the keyring.
            return serialize($countryScheme);
        }

        default:{
            throw new UnsupportedOperationException("Unknown object name objectName, cannot retrieve from session.");
            break;
        }
    }
}

//Call this before anything else
function initSession(){
    session_name('projectName');
    session_start();
}

function setSessionVariable($name, $value){
    $_SESSION['projectName'][$name] = $value;
}

function getSessionVariable($name, $default = FALSE){

    if(isset($_SESSION['projectName'])){
        if(isset($_SESSION['projectName'][$name])){
            $value = $_SESSION['projectName'][$name];
        }
    }
    return $default;
}

然后通过调用检索该对象

getSessionObject('gnugpg_key_ring');

但是,并非所有对象总是可序列化的,例如,如果该对象持有打开的文件句柄文件,需要一些额外的代码来在对象序列化时关闭文件,然后在对象反序列化时重新打开文件。

如果对象很大,那么最好使用适当的缓存工具(例如 memcached)来存储序列化对象,而不是会话。

If it's a small object that doesn't take up much memory and is serializable you could just store it in the session:

function    getSessionObject($objectName, $params){

    $sessionObjectSerialized = getSessionVariable($objectName, FALSE);

    if($sessionObjectSerialized == FALSE){
        $sessionObjectSerialized = constructSessionObject($objectName, $params);
        setSessionVariable($objectName, $sessionObjectSerialized);
    }

    $sessionObject = unserialize($sessionObjectSerialized);

    return $sessionObject;
}


function    constructSessionObject($objectName, $params = array()){

    switch($objectName){

        case('gnugpg_key_ring'):{
            $gnugpgKeyRing = getGNUPGKeyRing(); //do whatever you need to do to make the keyring.
            return serialize($countryScheme);
        }

        default:{
            throw new UnsupportedOperationException("Unknown object name objectName, cannot retrieve from session.");
            break;
        }
    }
}

//Call this before anything else
function initSession(){
    session_name('projectName');
    session_start();
}

function setSessionVariable($name, $value){
    $_SESSION['projectName'][$name] = $value;
}

function getSessionVariable($name, $default = FALSE){

    if(isset($_SESSION['projectName'])){
        if(isset($_SESSION['projectName'][$name])){
            $value = $_SESSION['projectName'][$name];
        }
    }
    return $default;
}

and then retrieve that object by calling

getSessionObject('gnugpg_key_ring');

However not all objects are always serializable e.g. if the object holds a file handle to an open file, that would need to have some extra code to close the file when the object is serialized and then re-open the file when the object was unserialized.

If the object is large, then you would be better off using a proper caching tool like memcached to store the serialized object, rather than the session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文