Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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) . '
';
```