【Laravel3.0.0源码阅读分析】数据库缓存类database.php
发布日期:2021-06-30 20:44:48 浏览次数:2 分类:技术文章

本文共 1913 字,大约阅读时间需要 6 分钟。

key = $key; } /** * Determine if an item exists in the cache. * 确定缓存中是否存在指定键的项 * @param string $key * @return bool */ public function has($key) { return ( ! is_null($this->get($key))); } /** * Retrieve an item from the cache driver. * 从缓存驱动程序中检索项 * @param string $key * @return mixed */ protected function retrieve($key) { $cache = $this->table()->where('key', '=', $this->key.$key)->first(); if ( ! is_null($cache)) { if (time() >= $cache->expiration) return $this->forget($key); // unserialize-反序列化 return unserialize($cache->value); } } /** * Write an item to the cache for a given number of minutes. * 向缓存中写入一个带有过期时间的项 * * // Put an item in the cache for 15 minutes * Cache::put('name', 'Taylor', 15); * * * @param string $key * @param mixed $value * @param int $minutes * @return void */ public function put($key, $value, $minutes) { $key = $this->key.$key; $value = serialize($value); $expiration = $this->expiration($minutes); // To update the value, we'll first attempt an insert against the // database and if we catch an exception, we'll assume that the // primary key already exists in the table and update. // 要更新该值,我们将首先尝试对数据库进行插入,如果我们捕捉到异常,我们将假设主键已存在于表中并进行更新。 try { $this->table()->insert(compact('key', 'value', 'expiration')); } catch (\Exception $e) { $this->table()->where('key', '=', $key)->update(compact('value', 'expiration')); } } /** * Delete an item from the cache. * 从缓存中删除一个项 * @param string $key * @return void */ public function forget($key) { $this->table()->where('key', '=', $this->key.$key)->delete(); } /** * Get a query builder for the database table. * 获取数据库表的查询构建器。 * @return Laravel\Database\Query */ protected function table() { $connection = DB::connection(Config::get('cache.database.connection')); return $connection->table(Config::get('cache.database.table')); }}

github地址:    

转载地址:https://liushilong.blog.csdn.net/article/details/117429116 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Laravel3.0.0源码阅读分析】缓存驱动类driver.php
下一篇:【Laravel3.0.0源码阅读分析】APC缓存类apc.php

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月13日 15时52分50秒