Giter VIP home page Giter VIP logo

do-file-cache's Introduction

DO File Cache

Build Status Coverage Status StyleCI Packagist

DO File Cache is a PHP File-based Caching Library.

Its syntax is designed to closely resemble the PHP memcache extension.

Installation

You can easily install with composer. Just run composer require divineomega/do-file-cache.

If you need a PSR-6 cache, check out the PSR-6 Adapter for DO File Cache.

Usage

This section will show you how to use DO File Cache. If you have used memcache before, this should be pretty familiar.

Setup & Configuration

Before you can do anything with DO File Cache, you must instantiate it and then, if you wish, set some configuration options.

require_once "vendor/autoload.php";

$cache = new \DivineOmega\DOFileCache\DOFileCache();

$cache->changeConfig(["cacheDirectory" => "/tmp/DOFileCacheStorage/"]);

This code creates a new DO File Cache object called $cache and then configures it to store its cache files in the /tmp/DOFileCacheStorage/ directory.

There are several different configuration variables you can override. The table below describes them.

Option Description Default
cacheDirectory The directory in which you wish the cache files to be stored. We recommend you change this to a site-specific directory and ensure it is outside of the web root. You must include a trailing slash. /tmp/DOFileCacheStorage/
gzipCompression Whether or not to compress cache files using gzip. Unless you are storing very small values in your cache files, we recommend you leave this enabled. true
fileExtension The file extension that will be appended to all your cache files. cache
unixLoadUpperThreshold If your server's load is greater than this value, cache files will be returned regardless of whether they have expired. This can be used to prevent cache files being regenerated when server load is high. If you do not wish to use this feature, set this option to -1. -1

Setting a cache item

Putting something in your file cache is easy. You just need to use the set method, as shown below.

$cache->set('nursery_rhyme',"Mary had a little lamb", strtotime('+ 1 day'));

The first parameter is the cache key, which uniquely references this cache item.

The second parameter is the cache value - what you wish to store in this cache item. This can be a string, integer, array, object or any type of serializable PHP variable.

The third paramter is the expiry time. It can be specified as a UNIX timestamp or as a number of seconds less than 30 days worth. Cache items will expire and not be retrievable when this time is reached.

Note that if you use dots, dashes, underscores or a few other special characters in your cache key, the created cache files will be put into a directory structure. For example, a cache key of objects.cars.redCar will be stored in objects/cars/redCar.cache. This is useful if you wish to categorise cache files and to prevent too many cache files building up in a single directory.

Getting a cache item

To get a cache item you've previously stored, you need to use the get method. An example of how to do this is shown below.

$var = $cache->get('nursery_rhyme');

The only parameter is the cache key you defined when setting the cache item. You can retrieve any cached variable in this way.

Other DO File Cache methods

The setting and retrieval of cache items are the most important parts of DO File Cache. In fact, the set and get methods are probably all you will need.

However, the library provides the following more advanced commands if you need them.

  • $cache->delete($key) - Delete a specific item from the cache.
  • $cache->flush() - Deletes all items from the cache.
  • $cache->replace($key, $content, $expiry) - Similar to the set method, but will only update a cache item's value if the cache item already exists.
  • $cache->increment($key, $offset) - Increment a numeric cache item value by the specified offset (or one if the offset is omitted).
  • $cache->decrement($key, $offset) - Decrements a numeric cache item value by the specified offset (or one if the offset is omitted).

do-file-cache's People

Contributors

divineomega avatar jbrooksuk avatar kirsty-gasston avatar peter279k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

do-file-cache's Issues

Error suppression

In the line, error suppression (@) is used:
https://github.com/DivineOmega/DO-File-Cache/blob/master/src/DOFileCache.php#L136

Unfortunately, upon heave use of this code, your error log will get cluttered with error messages. Surely the @ prevents errors to be written to standard output (/web browser), but errors are still written to custom error handlers and log files.

Hence, I propose to not use error suppression, and solve the real problem at hand here. Data is only serialized whenever it is not a string:
https://github.com/DivineOmega/DO-File-Cache/blob/master/src/DOFileCache.php#L52 , so to clarify: ints, bools and floats are serialized as well. My suggestion is to always serialize, strings too, and also always unserialize. This way the get procedure will not result in loads of notices in the log any more.

The only downside is that strings will have to be serialized, increasing some storage space and computation time. This does not outweight the benefit of having well-defined storage characteristics, imho.

Btw, I'm posting this as a user of the divineomega/password_exposed library, which is an example of this issue in practice.

Exception on Windows

Hi,
I am on Windows and therefore the sys_getloadavg() function does not exist and even though I haven't touched the default configuration "'unixLoadUpperThreshold' => -1," as the suggestion in the exception message reads, the exception is nevertheless thrown. I think this check

if (!function_exists('sys_getloadavg')) {

should be done instead before this line

$unixLoad = sys_getloadavg();
.

Best regards,
Dimitar

Using clearstatcache before checking file states/manipulations

According to clearstatcache function reference, the file state cache is effected on following related file functions:

Affected functions include stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), and fileperms().

To avoid file stat caching, it should use this function before manipulating any file operation :).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.