Rumah >pembangunan bahagian belakang >tutorial php >laravel4 缓存null值时cache失效
<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