>  기사  >  백엔드 개발  >  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으로 문의하세요.