Home > Backend Development > PHP Tutorial > laravel4 缓存null值时cache失效

laravel4 缓存null值时cache失效

WBOY
Release: 2016-06-06 20:38:05
Original
1127 people have browsed it

<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>
Copy after login
Copy after login

<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>
Copy after login
Copy after login

如果缓存保存的值时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>
Copy after login
Copy after login

<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>
Copy after login
Copy after login

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

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template