2012-07-01 10:54



I'm having a bit of trouble with Memcached.

I have this code below:

if(!$Z->get('user')) {
    $Z->set('user', $hs->load_hs_main($_GET['from'], $_GET['table']));

$je = $Z->get('user') ? $Z->get('user') : $hs->load_hs_main($_GET['from'], $_GET['table']);

What $Z->get('user') and $je are supposed to be is a MySQLi result object. This gives weird effects like print_r not functioning and it just doesn't look right.

I want to check if an object exists that was created by memcached and use it if it exists.. but if it doesn't, set it.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongya1875 dongya1875 9年前

    The correct way to get an item from cache or create/load it from somewhere else if it doesn't exist is this:

    $user = $Z->get('user'); // get it from cache, returns falsy value if not found
    if(!$user) { // not found
        $user = $hs->load_hs_main(...); // load the element from somewhere else
        $Z->set('user', $user); // cache it
    // here $user always exists
    点赞 评论 复制链接分享
  • dpps0715 dpps0715 9年前

    In general you can't store objects of "internal" classes in memcache. Those objects contain data which can't be serialized. You have to fetch the data from the result object and store it in a PHP array. Then you can store that array in memcache.

    点赞 评论 复制链接分享