企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 查询缓存 通常我们在使用select和find的时候,希望查询结果缓存下来,但是又需要调用cache(), 还要写判断语句,为了避免这种麻烦,HYPHP 直接在Model中内置了 查询缓存。 ## 通常使用Select ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ $User = S("User"); $User->select('user',array( "uid" =>1, )); } } ~~~ 上面的代码 select 获取user表 uid为1的用户 这是一个我们常用的查询方法。而且是固定的结果。 但是每次访问都必须执行这个SQL去获取。 所以会消耗服务器资源。 所以我们有必要将这个查询结果缓存下来。 ## 使用查询缓存 S()->cache($key,$expire=NULL)->select() ->cache(缓存键名,缓存过期时间)->select() ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ $User = S("User"); $User->cache('user',10)->select('user',array( "uid" =>1, )); } } ~~~ 上面的代码 缓存10秒这个select查询得到的数据 缓存键名为user ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ $User = S("User"); $User->cache(true,10)->select('user',array( "uid" =>1, )); } } ~~~ ->cache(true,10) 缓存键名填写 true 则会按照这条sql做 自动补充键名。 ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ $User = S("User"); $User->cache(true)->select('user',array( "uid" =>1, )); } } ~~~ 看到上面代码,cache没有输入过期时间。则该缓存会永久缓存