项目作者: ThemePlate

项目描述 :
Convenient caching methods
高级语言: PHP
项目地址: git://github.com/ThemePlate/Cache.git
创建时间: 2020-01-27T01:29:20Z
项目社区:https://github.com/ThemePlate/Cache

开源协议:GNU General Public License v3.0

下载


ThemePlate Cache

Usage

  1. use ThemePlate\Cache;
  2. Cache::remember( 'unique_key', function() {
  3. return expensive_task();
  4. }, MINUTE_IN_SECONDS );
  5. Cache::forget( 'unique_key' );
  6. Cache::file( 'special_key', 'path_to_file' );
  7. $processor = Cache::processor();
  8. $processor->report( function( $output ) {
  9. error_log( print_r( $output, true ) );
  10. } );
  11. function hourly_moment() {
  12. return 'to remember ' . time();
  13. }
  14. Cache::remember( 'unique_key', 'hourly_moment', HOUR_IN_SECONDS );

Force refresh value/s

<WP_HOME>/?tcs_refresh=<single_key>

<WP_HOME>/?tcs_refresh[]=<key1>&tcs_refresh[]=<key2>

works only when logged-in

Cache::remember( $key, $callback, $expiration )

Retrieve content from the cache or, if it doesn’t exist, execute $callback and its result is returned then saved

  • $key (string)(Required) Unique cache key to use
  • $callback (callable)(Required) Function that returns data to store
  • $expiration (int)(Optional) Number of seconds before entry expires. Default 0 (forever)

Cache::forget( $key, $default )

Retrieve and delete the cache

  • $key (string)(Required) Unique cache key to use
  • $default (mixed)(Optional) To return if cache doesn’t exist. Default null

Cache::file( $key, $path )

Like remember but, uses the file contents and no expiration, automatically updates if the file is modified instead

  • $key (string)(Required) Unique cache key to use
  • $path (string)(Required) Path of the file to read

Cache::processor()

Support for soft-expiration, Cache::remember* and Cache::file updates in the background

*Except for using anonymous function as callback (closure)