FileCache
in package
Library of functions used to implement a simple file cache
Tags
Table of Contents
- MAX_FILES_IN_A_BIN = 5000
- Maximum number of files in a bin
- NUMBER_OF_BINS = 50
- Total number of bins to cycle between
- $cache_file : string
- File used to serve last cache request
- $dir_name : string
- Folder name to use for this FileCache
- $web_site : object
- Reference to WebSite object that the current code is running under
- __construct() : mixed
- Creates the directory for the file cache, sets how frequently all items in the cache expire
- checksum() : int
- Makes a 0 - self::NUMBER_OF_BINS value out of the provided key
- clear() : mixed
- Deletes cache key value files and ram copies of key values stored in the this file cache
- fileGetContents() : string
- Either a wrapper for file_get_contents, or if a WebSite object is being used to serve pages, it reads it in using blocking I/O file_get_contents() and caches it before return its string contents.
- filePutContents() : mixed
- Either a wrapper for file_put_contents, or if a WebSite object is being used to serve pages, writes $data to the persistent file with name $filename. Saves a copy in the RAM cache if there is a copy already there.
- get() : mixed
- Retrieve data associated with a key that has been put in the cache
- set() : mixed
- Stores in the file cache a key-value pair
- updateCache() : mixed
- Used to mark a cache item, and keep track of rounds according to the marker algorithm. This function determine if the cache is too full and if so eject an item.
Constants
MAX_FILES_IN_A_BIN
Maximum number of files in a bin
public
mixed
MAX_FILES_IN_A_BIN
= 5000
NUMBER_OF_BINS
Total number of bins to cycle between
public
mixed
NUMBER_OF_BINS
= 50
Properties
$cache_file
File used to serve last cache request
public
string
$cache_file
$dir_name
Folder name to use for this FileCache
public
string
$dir_name
$web_site
Reference to WebSite object that the current code is running under
public
object
$web_site
Methods
__construct()
Creates the directory for the file cache, sets how frequently all items in the cache expire
public
__construct(string $dir_name[, mixed $web_site = null ]) : mixed
Parameters
- $dir_name : string
-
folder name of where to put the file cache
- $web_site : mixed = null
Return values
mixed —checksum()
Makes a 0 - self::NUMBER_OF_BINS value out of the provided key
public
checksum(string $key) : int
Parameters
- $key : string
-
to convert to a random value between 0 - self::NUMBER_OF_BINS
Return values
int —value between 0 and self::NUMBER_OF_BINS
clear()
Deletes cache key value files and ram copies of key values stored in the this file cache
public
clear() : mixed
Return values
mixed —fileGetContents()
Either a wrapper for file_get_contents, or if a WebSite object is being used to serve pages, it reads it in using blocking I/O file_get_contents() and caches it before return its string contents.
public
fileGetContents(string $filename[, bool $force_read = false ]) : string
Note this function assumes that only the web server is performing I/O with this file. filemtime() can be used to see if a file on disk has been changed and then you can use $force_read = true below to force re- reading the file into the cache
Parameters
- $filename : string
-
name of file to get contents of
- $force_read : bool = false
-
whether to force the file to be read from persistent storage rather than the cache
Return values
string —contents of the file given by $filename
filePutContents()
Either a wrapper for file_put_contents, or if a WebSite object is being used to serve pages, writes $data to the persistent file with name $filename. Saves a copy in the RAM cache if there is a copy already there.
public
filePutContents(string $filename, string $data) : mixed
Parameters
- $filename : string
-
name of file to write to persistent storages
- $data : string
-
string of data to store in file
Return values
mixed —get()
Retrieve data associated with a key that has been put in the cache
public
get(string $key) : mixed
Parameters
- $key : string
-
the key to look up
Return values
mixed —the data associated with the key if it exists, false otherwise
set()
Stores in the file cache a key-value pair
public
set(string $key, mixed $value) : mixed
Parameters
- $key : string
-
to associate with value
- $value : mixed
-
to store
Return values
mixed —updateCache()
Used to mark a cache item, and keep track of rounds according to the marker algorithm. This function determine if the cache is too full and if so eject an item.
protected
updateCache(string $key) : mixed
Parameters
- $key : string
-
that was just read from or written to. Might need to be marked according to Marker algorithm