themeplate / cache
方便的片段缓存方法
v2.6.2
2024-05-20 04:06 UTC
README
用法
use ThemePlate\Cache; Cache::remember( 'unique_key', function() { return expensive_task(); }, MINUTE_IN_SECONDS ); Cache::forget( 'unique_key' ); Cache::file( 'special_key', 'path_to_file' ); $processor = Cache::processor(); $processor->report( function( $output ) { error_log( print_r( $output, true ) ); } ); function hourly_moment() { return 'to remember ' . time(); } Cache::remember( 'unique_key', 'hourly_moment', HOUR_IN_SECONDS );
强制刷新值
<WP_HOME>/?tcs_refresh=<single_key>
<WP_HOME>/?tcs_refresh[]=<key1>&tcs_refresh[]=<key2>
仅当登录时生效
Cache::remember( $key, $callback, $expiration )
从缓存中检索内容或,如果不存在,则执行 $callback 并返回其结果,然后保存
- $key (string)(Required) 使用唯一缓存键
- $callback (callable)(Required) 返回要存储的数据的函数
- $expiration (int)(Optional) 条目过期前的秒数。默认 0 (永远)
Cache::forget( $key, $default )
检索并删除缓存
- $key (string)(Required) 使用唯一缓存键
- $default (mixed)(Optional) 缓存不存在时返回。默认
null
Cache::file( $key, $path )
类似于 remember,但使用文件内容,没有过期时间,文件修改时自动更新
- $key (string)(Required) 使用唯一缓存键
- $path (string)(Required) 要读取的文件路径
Cache::processor()
支持软过期,Cache::remember* 和 Cache::file 在后台更新
*除了使用匿名函数作为回调(闭包)之外