PHP Memcache 在构造函数中调用

发布于 2024-11-15 13:09:44 字数 1546 浏览 4 评论 0原文

我正在更新一些对象模型以将其数据缓存到 memcache,然后如果找不到缓存键,则回退到常规的旧 SQL 查询。最初,构造函数看起来像这样,当然虽然经过了简化。

function __construct($id){
    $sql = 'SELECT name, place FROM Objs WHERE ObjID = ?';
    $params = array($id);
    $data = Database::GetRow($sql, $params);

    $this->name = $data['name'];
    $this->place = $data['place'];
}

我已将其修改为如下内容:

function __construct($id){
    $data = $Memcache->get('obj-'.$id);
    if(empty($data)){
        $sql = 'SELECT name, place FROM Objs WHERE ObjID = ?';
        $params = array($id);
        $data = Database::GetRow($sql, $params);
        $Memcache->set('obj-'.$id, $data, false, 350);
    }

    $this->name = $data['name'];
    $this->place = $data['place'];
}

这一切都很好,但是许多对象具有其他私有方法来执行其他查询并设置更多属性。我希望能够做的是在设置属性之后从 memcached 返回完整的对象。问题是,我不确定如何执行此操作,因为构造函数无法分配 $this 关键字。

function __construct($id){
    $data = $Memcache->get('obj-'.$id);
    if(empty($data)){
        $sql = 'SELECT name, place FROM Objs WHERE ObjID = ?';
        $params = array($id);
        $data = Database::GetRow($sql, $params);
        $this->name = $data['name'];
        $this->place = $data['place'];
        $Memcache->set('obj-'.$id, $data, false, 350);
    }else{
        //this is definitely not right, though semantically it kinda defines what I want to accomplish
        $this = $data
    }
}

无论如何,是否可以将这种逻辑保留在构造函数中,并以某种方式优雅地使从 memcached 返回的对象“成为”构造函数接收的实例?或者我是否需要在首先检查内存缓存的函数中为构造函数添加别名?

Im in the process of updating some object models to cache their data to memcache and then fall back to the regular old sql query if no cache key is found. Originally, the constructors look something like this, though simplified of course.

function __construct($id){
    $sql = 'SELECT name, place FROM Objs WHERE ObjID = ?';
    $params = array($id);
    $data = Database::GetRow($sql, $params);

    $this->name = $data['name'];
    $this->place = $data['place'];
}

Which I have modified to something like this:

function __construct($id){
    $data = $Memcache->get('obj-'.$id);
    if(empty($data)){
        $sql = 'SELECT name, place FROM Objs WHERE ObjID = ?';
        $params = array($id);
        $data = Database::GetRow($sql, $params);
        $Memcache->set('obj-'.$id, $data, false, 350);
    }

    $this->name = $data['name'];
    $this->place = $data['place'];
}

This is all fine and good, but many of the objects have other private methods that perform other queries and set more attributes. What I'd like to be able to do is have the object in full be returned from memcached, AFTER properties are set. The problem is, I'm not sure how to do this, since the constructor can't assign the $this keyword.

function __construct($id){
    $data = $Memcache->get('obj-'.$id);
    if(empty($data)){
        $sql = 'SELECT name, place FROM Objs WHERE ObjID = ?';
        $params = array($id);
        $data = Database::GetRow($sql, $params);
        $this->name = $data['name'];
        $this->place = $data['place'];
        $Memcache->set('obj-'.$id, $data, false, 350);
    }else{
        //this is definitely not right, though semantically it kinda defines what I want to accomplish
        $this = $data
    }
}

Is there anyway to keep this logic in the constructor and somehow gracefully have the objected returned from memcached "become" the instance that the constructor receives? Or will I need to alias the constructor within a function that checks memcache first?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文