首頁  >  文章  >  後端開發  >  laravel4 缓存null值时cache失效

laravel4 缓存null值时cache失效

WBOY
WBOY原創
2016-06-06 20:38:051088瀏覽

<code>public static function getUserById($id)
{
    $key = sprintf("%s_%s",__METHOD__, $id);

    $mdkey = md5($key);

    var_dump(Cache::tags(self::CACHE_TAG)->has($mdkey));die();

    if(Cache::tags(self::CACHE_TAG)->has($mdkey))
    {
        echo 'has cache';
        return Cache::tags(self::CACHE_TAG)->get($mdkey);
    }

    $user = User::where('uid',$id)->first();

    Cache::tags(self::CACHE_TAG)->put($mdkey, $user, self::CACHE_TIME);

    return $wifi;
}
</code>

<code>在Repository中的has方法定义
/**
 * Determine if an item exists in the cache.
 *
 * @param  string  $key
 * @return bool
 */
public function has($key)
{
    return ! is_null($this->get($key));
}
</code>

如果缓存保存的值时null时,has都会返回false,如何区分用户保存的null和cache无保存时的null呢?

回复内容:

<code>public static function getUserById($id)
{
    $key = sprintf("%s_%s",__METHOD__, $id);

    $mdkey = md5($key);

    var_dump(Cache::tags(self::CACHE_TAG)->has($mdkey));die();

    if(Cache::tags(self::CACHE_TAG)->has($mdkey))
    {
        echo 'has cache';
        return Cache::tags(self::CACHE_TAG)->get($mdkey);
    }

    $user = User::where('uid',$id)->first();

    Cache::tags(self::CACHE_TAG)->put($mdkey, $user, self::CACHE_TIME);

    return $wifi;
}
</code>

<code>在Repository中的has方法定义
/**
 * Determine if an item exists in the cache.
 *
 * @param  string  $key
 * @return bool
 */
public function has($key)
{
    return ! is_null($this->get($key));
}
</code>

如果缓存保存的值时null时,has都会返回false,如何区分用户保存的null和cache无保存时的null呢?

"\0"之类的占位符代替null

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn