devuri/ob-cache
最新稳定版本:0.1.0
Composer 安装命令:
composer require devuri/ob-cache
包简介
A simple versatile and easy-to-use PHP class designed for managing caching in WordPress environments.
README 文档
README
Overview
The ObCache class is a versatile and easy-to-use PHP class designed for caching in WordPress environments. It provides functionality to manage caching operations with ease, supporting operations such as setting, getting, and removing cached data. This class is particularly useful in scenarios where data retrieval from a cache is preferred over repeatedly querying a database or performing complex computations.
Features
- Flexible Caching Control: Control whether caching is enabled or disabled.
- Easy Initialization: Instantiation via a constructor or a static
initmethod. - Data Storage and Retrieval: Methods for setting and retrieving cached data.
- Cache Removal: Functionality to remove specific cache entries.
Usage
Instantiation
Directly via constructor:
$cache = new ObCache();
Using the static init method:
$cache = ObCache::init();
Setting Cache Mode
Enable or disable caching:
$cache->set_cache_allowed(true); // Enable caching $cache->set_cache_allowed(false); // Disable caching
Setting Data in Cache
Use the set method to cache data:
$cache->set('cache_key', function() { // Data generation logic return $data; }, 3600); // 3600 seconds expiration
Retrieving Data from Cache
Use the get method to retrieve or generate and cache data:
$data = $cache->get('cache_key', function() { // Data generation logic return $data; }, 3600);
Removing Data from Cache
Use the forget method to remove data from the cache:
$cache->forget('cache_key');
Integration with WordPress
This class utilizes WordPress caching functions (wp_cache_set, wp_cache_get, wp_cache_delete) and is designed to work within a WordPress environment.
Notes
- The class uses a protected constant
OBC_CACHE_GROUPto define a cache group calledevp_cachedfor better organization and management of cached items. - Cache mode status can be checked using
is_cache_allowed()method. - When caching is disabled, the
setandgetmethods directly handle data without caching.
License
This project is licensed under the MIT License - see the LICENSE file for details.
统计信息
- 总下载量: 3.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-17