Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/co0lc0der/simple-cache
A simple cache php component powered by text files
https://github.com/co0lc0der/simple-cache
cache caching php php-oop
Last synced: about 1 month ago
JSON representation
A simple cache php component powered by text files
- Host: GitHub
- URL: https://github.com/co0lc0der/simple-cache
- Owner: co0lc0der
- License: mit
- Created: 2022-05-01T18:20:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-05T10:41:47.000Z (over 1 year ago)
- Last Synced: 2024-11-14T00:09:11.126Z (about 1 month ago)
- Topics: cache, caching, php, php-oop
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Cache php component
This is easy-to-use php component to add caching in your project. See `index.php` for examples.
### Public methods:
- `getInstance()` - inits the component
- `set()` - sets a cache for the key for some period
- `get()` - returns cached data by the key
- `getOrSet()` - sets a cache if it doesn't exist and returns it or returns cached data
- `delete()` - deletes a cache file for the key
- `clear()` - erases all cache data
## How to use
### 1. Include Cache class and init it. It uses `cache` folder by default, but you can change the path.
```php
require_once 'Cache/Cache.php';$cache = Cache::getInstance('./runtime/cache');
```
### 2. Use `get()`, `set()` or `getOrSet()` methods with keys you need.
```php
$reviews = $cache->get('reviews');if (!$reviews) {
$reviews = (new Review())->getAll();
$cache->set('reviews', $reviews);
}
```
or
```php
if (!$info = $cache->get('server_info')) {
$info = $_SERVER;
$cache->set('server_info', $info);
}
```
or
```php
$reviews = $cache->getOrSet('reviews', function() {
return (new Review())->getAll();
});
```
### 3. Do something with data you've got.
```php
foreach($reviews as $review) {
echo "{$review['name']}
{$review['text']}";
}
```
or
```php
echo '' . print_r($info, true) . '';
```