carloshb/cache-service

Simple cache service using SQLite to reduce multiple db queries

0.2.1 2017-05-23 01:24 UTC

This package is not auto-updated.

Last update: 2024-11-10 04:01:50 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Simple cache service using SQLite to reduce multiple db queries.

Basic information

  • default cache time: 60mins
  • default cache path: Carloshb\CacheService\Driver

How to install?

Install using composer

composer require carloshb/cache-service 0.2.*

Or clone in or project.

How to change default configs?

Add PHP env:

    $_ENV['cache_time'] = 10; // minutes
    $_ENV['cache_path'] = 'yor/storage/path';

Usage

resolve(string $key, callable $callback [, int $time = 60]) : Cache

The method resolve() returns an instance of Cache class and generate or update the key sent with the callback return. First parameter is the key, a string. Second parameter is the callback, a function returning array with expected results. Third parameter is optional, for set n minutes for cache.

    $cache = new \Carloshb\CacheService\Cache();
    $cache->resolve('getInfo', function(){
        return array(
            'name' => 'Carlos Henrique Bernardes',
            'email' => 'contato@carloshb.com.br'
        );
    });

getCacheContent() : json

Use after resolve() to get a content of cache, the method returns a json.

    $cache = new \Carloshb\CacheService\Cache();
    $content = $cache->resolve('getInfo', function(){
        return array(
            'name' => 'Carlos Henrique Bernardes',
            'email' => 'contato@carloshb.com.br'
        );
    })->getCacheContent();

destroy(string $key) : bool

Use this method to force cache destroy using the key name.

    $cache = new \Carloshb\CacheService\Cache();
    $response = $cache->destroy('getInfo');
    var_dump($response); // true or false