Laravel - 高速缓存口才与频繁的更新 [英] Laravel - Caching Eloquent with Frequent Updates

查看:184
本文介绍了Laravel - 高速缓存口才与频繁的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在经常修改的对象上使用缓存吗?例如,假设我们有一个BlogPost对象,并且有一个num_of_views列(等)经常发生变化。是否可以在缓存和数据库中更新num_of_views字段,而不会破坏缓存对象并重新创建它?我可以手动执行,但我担心同步问题。

Is it possible to use caching on an object that will be frequently modified? For instance, let's say we have a BlogPost object, and there's a num_of_views column (amongst others) that frequently changes. Is it possible to update the num_of_views field, both in cache and in the DB, without destroying the cache object and recreating it? I can do it manually, however I worry about sync issues.

推荐答案

是的,是的。我不知道你如何做缓存,但是您可以随时替换缓存实例:

Yes, it is. I don't know how you are doing cache, but you can replace a cache instance at any time:

public function updatePost($post_id, $num_of_views)
{
    if (Cache::has('POST.'.$post_id))
    {
        $post = Cache::get('POST.'.$post_id);
    }
    else
    {
        $post = Post::find($post_id);
    }

    $post->num_of_views = $num_of_views;

    $post->save();

    Cache::put('POST.'.$post_id, $post);
}

这篇关于Laravel - 高速缓存口才与频繁的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆