MemcacheD每秒可以节省多个数据

时间:2016-06-17 17:38:12

标签: php laravel memcached

快乐星期五老朋友Stack Overflow用户!

让我稍微设置一下场景:

我有一个应用程序,使用以下代码将用户操作记录到MemcacheD中:

<?php
    // This tracks a users actions for 24 hours
    function listActions($user){
        $minutes = 1440;    $total
        $actionsCache = Cache::remember('userActions', $minutes, function() use($total) {
            $array = [100, 140, 523, 10, 109]; // example list of user ID's
            $ar = [];
            foreach($array as $p){
                $array[$p] = [];
            }
            return $ar;
        });
        return $actionsCache;
    }
?>

然后,每当用户在网站中执行操作时,都会使用以下内容进行记录:

<?php
    // This records a new action and then saves it to the userActions cache
    function saveAction($action, $user){
        $minutes = 1440;
        $Cache = Cache::get('userActions');
        $Cache[$user][time()] = $action;
        Cache::put('userActions', $Cache, $minutes);
        return $Cache[$user];
    }

?>

这是一个简化的形式,但它很好地说明了我的观点。

我的问题:好像有些动作没有被录制。这些操作发生得相对较快,并且对每个用户的每个操作都操作相同的Cache变量(如上所示)

我的问题:代码是否可能无法获取最新的缓存值,而是保存旧值?

1 个答案:

答案 0 :(得分:0)

I ended up fixing this issue by switching over to redis, as it has locking built in.

If I had stayed with MemcacheD (which I did for a while) I would have implemented a lock on the data structure itself so it could not be edited while another process was utilizing it (mainly, anything that would have read AND wrote to the data, would have needed to lock the data).

I hope this helps anyone that may come by this!