https://github.com/devuri/ob-cache
The ObCache class is a versatile and easy-to-use PHP class designed for caching in WordPress environments.
https://github.com/devuri/ob-cache
packagist php php-library
Last synced: 11 months ago
JSON representation
The ObCache class is a versatile and easy-to-use PHP class designed for caching in WordPress environments.
- Host: GitHub
- URL: https://github.com/devuri/ob-cache
- Owner: devuri
- License: mit
- Created: 2023-12-17T16:23:48.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-17T17:41:30.000Z (about 2 years ago)
- Last Synced: 2024-10-19T22:07:14.398Z (over 1 year ago)
- Topics: packagist, php, php-library
- Language: PHP
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# README for the `ObCache` Class
## 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 `init` method.
- **Data Storage and Retrieval**: Methods for setting and retrieving cached data.
- **Cache Removal**: Functionality to remove specific cache entries.
## Usage
### Instantiation
Directly via constructor:
```php
$cache = new ObCache();
```
Using the static `init` method:
```php
$cache = ObCache::init();
```
### Setting Cache Mode
Enable or disable caching:
```php
$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:
```php
$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:
```php
$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:
```php
$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_GROUP` to define a cache group called `evp_cached` for better organization and management of cached items.
- Cache mode status can be checked using `is_cache_allowed()` method.
- When caching is disabled, the `set` and `get` methods directly handle data without caching.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.