TP的缓存主要依赖Cache类,Cache类其实是一个代理类,Cache类通过getInstance静态方法来获取缓存实例,而getInstance方式实际是调用Cache类的connect方法,该方法类似于工厂返回对应缓存类型的驱动实例,这里以Redis为例,那么这里返回的就是CacheRedis驱动,该驱动在构造函数中会实例Redis扩展类,并赋值给handle属性来做本地代理,所以我们可以像使用Redis类一样的使用CahceRedis,并重写get和set方法,提供对外的统一接口.
查询缓存
查询缓存使用Model的cache方法来实现缓存处理,cache方法接受3个参数,分别为:缓存的键,缓存时间,缓存类型,并返回当前Model的实例$this,在这里Model只是将缓存信息存入options属性,之后DB类在select方法中会检测是否使用缓存,并调用S方法来做缓存的存取,S方法实际上则是使用Cache类来做存取
缓存的键:可以为字符串或者true,如果为true,那么TP会根据生成的SQL语句的MD5值作为key,缓存前缀读取配置DATA_CACHE_PREFIX
缓存时间:以秒为单位,如果不设置则读取配置文件中DATA_CACHE_TIME的值
缓存类型:默认为文件缓存,可选类型aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASQAAADsCAIAAABSV0zWAAAOB0lEQVR4nO2dwW7ktgGG81wDdeLdIA3aS9F1jD0EQYKghw7GaA85tWhn/AK7DWChaLI5ZnRJfOxFRp6qL9CDZiSKpDSasUTxpz7jQ+BlJJKS+VmURvz9UfbxKwAIwEez9wBgISAbQCCQDSAQyAYQiECy/frvbyz+sX0z+8EDhGQ22QL4tlrfP5bF7u5m9rMMkM0rmxd339X6/rEsn5+fn5+fy/x+eKPIBlFxgWyHw+HJ9/XTTz+d3fdq2Vbb/Pn5+XF7U//zsH87sMPIBlFxgWxff/Mnr2xffe25HFl0XbX6t6muabVpl4JsEBWXTSM//PijZdoPHz4M2fFK2bZ5WTzcrj22rO4eDqe5ZXPdMyach/3bo2z73JqCWpvN/jOAhXCZbH9887kl2x/e3A7ZcVzZVuu3u/xYvtrmZZlv1jeVQqY8VUnl2Oru4VAWu7sb83K3Wr/dFddfOQEu4uIHJO+/+6427d279wP3Gv3Kln386nZfHC9ZlWzO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TkSuQmCC" alt="" />,这是TP默认支持的缓存驱动,也可自己扩展,如果不设置该值则读取配置DATA_CACHE_TYPE
Redis相关配置
REDIS_HOST 主机
REDIS_PORT 端口