Skip to content

co0lc0der/simple-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

require_once 'Cache/Cache.php';

$cache = Cache::getInstance('./runtime/cache');

2. Use get(), set() or getOrSet() methods with keys you need.

$reviews = $cache->get('reviews');

if (!$reviews) {
  $reviews = (new Review())->getAll();
  $cache->set('reviews', $reviews);
}

or

if (!$info = $cache->get('server_info')) {
  $info = $_SERVER;
  $cache->set('server_info', $info);
}

or

$reviews = $cache->getOrSet('reviews', function() {
  return (new Review())->getAll();
});

3. Do something with data you've got.

foreach($reviews as $review) {
  echo "<h2>{$review['name']}</h2><div>{$review['text']}</div>";
}

or

echo '<pre>' . print_r($info, true) . '</pre>';

About

A simple cache php component powered by text files

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages