codeliter/cacheadapter

An adapter for using the Nette Caching Component

1.3.3 2019-03-05 02:11 UTC

This package is auto-updated.

Last update: 2024-04-05 18:01:34 UTC


README

This library does not implement any functionality on its own it is written to help communicate better with the Nettecache library. It is only an adapter that streamlines caching using memcached or file storage.

Requirements

  • Php >= 5.4

  • Composer

  • Php Memcached extension(Optional)

Installation

  • Open your default terminal program

  • Change your working directory to the project directory you wish to use this library in.

    You can run the following the command on mac/linux.

    cd /var/www/html/<project-directory>
    

    is where your project lives in

  • Run the command to import the library

    composer require codeliter/cacheadapter
    
  • That's all

Usage

Using this library can be as easy as:

  • Initialize the Cache Library and set the absolute or relative path of where you want the cache to be saved to

    -If your server has Memcached loaded it uses memcached by default.

      $cache = new Codeliter\CacheAdapter\Cache('./data/cache');
    
  • You can save anything to Cache like this

      $cache_name = 'test_cache';
      $data = 'This is the content of the test_case';
      $cache->save($cache_name, $data, '4 Hours');
    
  • You can get anything from the Cache

      $cache_name = 'test_cache';
      $data = $cache->get($cache_name);
    
  • You can delete from Cache

      $cache_name = 'test_cache';
      $cache->delete($cache_name);
    
  • A typical use would be

    • Make an API or database request and store it in the cache for as long as the data would not change

        $cache_name = 'google_home_page';
        
        try {
            // Check if the data already exists
            $data = $cache->get($cache_name);
        }
        catch (Throwable $t) {
            // If data does not exist
            // Make the API or Database request
            $request = file_get_contents('https://google.com');
            // Store the data to cache for sometime for future requests
            $cache->save($cache_name, $request, '24 Hours');
        }
      

Credits