Postgres
extends CacheApi
in package
implements
CacheApiInterface
PostgreSQL Cache API class
Table of Contents
Interfaces
Constants
- APIS_DEFAULT = 'FileBased'
- APIS_FOLDER = __DIR__ . '/APIs'
- APIS_NAMESPACE = __NAMESPACE__ . '\APIs\\'
Properties
- $accelerator : string
- $count_hits : int
- $count_misses : int
- $enable : int
- $hits : array<string|int, mixed>
- $loadedApi : object|bool
- $misses : array<string|int, mixed>
- $min_smf_version : string
- $prefix : string
- $ttl : int
- $version_compatible : string
- $backcompat : array<string|int, mixed>
- $db_connection : resource
- $db_prefix : string
Methods
- __construct() : mixed
- Does basic setup of a cache method when we create the object but before we call connect.
- cacheSettings() : void
- Specify custom settings that the cache API supports.
- clean() : void
- Empty out the cache in use as best it can
- cleanCache() : bool
- Clean out the cache.
- connect() : bool
- Connects to the cache method. This defines our $key. If this fails, we return false, otherwise we return true.
- detect() : array<string|int, mixed>
- Get the installed Cache API implementations.
- exportStatic() : void
- Provides a way to export a class's public static properties and methods to global namespace.
- get() : array<string|int, mixed>|null
- Gets the value from the cache specified by key, so long as it is not older than ttl seconds.
- getCompatibleVersion() : string
- Gets the latest version of SMF this is compatible with.
- getData() : mixed
- Retrieves an item from the cache.
- getDefaultTTL() : int
- Gets the TTL as defined from set or the default.
- getImplementationClassKeyName() : string
- Gets the class identifier of the current caching API implementation.
- getMinimumVersion() : string
- Gets the min version that we support.
- getPrefix() : string
- Gets the prefix as defined from set or the default.
- getVersion() : string
- Gets the Version of the Caching API.
- housekeeping() : void
- Run housekeeping of this cache exp. clean up old data or do optimization
- invalidateCache() : bool
- Invalidate all cached data.
- isSupported() : bool
- Checks whether we can use the cache method performed by this API.
- load() : object|false
- Try to load up a supported caching method.
- put() : void
- Puts value in the cache under key for ttl seconds.
- putData() : mixed
- Stores a value, regardless of whether or not the key already exists (in which case it will overwrite the existing value for that key).
- quickGet() : string
- Try to retrieve a cache entry. On failure, call the appropriate function.
- quit() : bool
- Closes connections to the cache method.
- setDefaultTTL() : bool
- Sets a default Time To Live, if this isn't specified we let the class define it.
- setPrefix() : bool
- Sets the cache prefix.
- createTempTable() : void
- Create the temp table of valid data.
- deleteTempTable() : void
- Delete the temp table.
- prepareQueries() : mixed
- Stores a prepared SQL statement, ensuring that it's not done twice.
- retrieveData() : void
- Retrieve the valid data from temp table.
Constants
APIS_DEFAULT
public
mixed
APIS_DEFAULT
= 'FileBased'
APIS_FOLDER
public
mixed
APIS_FOLDER
= __DIR__ . '/APIs'
APIS_NAMESPACE
public
mixed
APIS_NAMESPACE
= __NAMESPACE__ . '\APIs\\'
Properties
$accelerator
public
static string
$accelerator
Name of the selected cache engine.
This is an copy of the $cache_accelerator setting in Settings.php.
$count_hits
public
static int
$count_hits
= 0
The number of times the cache has been accessed.
For backward compatibility, also referenced as global $cache_count.
$count_misses
public
static int
$count_misses
= 0
The number of times the cache has missed.
For backward compatibility, also referenced as global $cache_count_misses.
$enable
public
static int
$enable
Caching level. Values range from 0 to 3.
This is an copy of the $cache_enable setting in Settings.php.
$hits
public
static array<string|int, mixed>
$hits
= []
Records debugging info.
For backward compatibility, also referenced as global $cache_hits.
$loadedApi
public
static object|bool
$loadedApi
The loaded cache API, or false on failure.
For backward compatibility, also referenced as global $cacheAPI.
$misses
public
static array<string|int, mixed>
$misses
= []
Records debugging info.
For backward compatibility, also referenced as global $cache_misses.
$min_smf_version
protected
string
$min_smf_version
= '2.1 RC1'
The minimum SMF version that this will work with.
$prefix
protected
string
$prefix
= ''
The prefix for all keys.
$ttl
protected
int
$ttl
= 120
The default TTL.
$version_compatible
protected
string
$version_compatible
= '3.0.999'
The maximum SMF version that this will work with.
$backcompat
private
static array<string|int, mixed>
$backcompat
= ['prop_names' => ['loadedApi' => 'cacheAPI', 'hits' => 'cache_hits', 'count_hits' => 'cache_count', 'misses' => 'cache_misses', 'count_misses' => 'cache_count_misses']]
BackwardCompatibility settings for this class.
$db_connection
private
resource
$db_connection
result of pg_connect.
$db_prefix
private
string
$db_prefix
Methods
__construct()
Does basic setup of a cache method when we create the object but before we call connect.
public
__construct() : mixed
cacheSettings()
Specify custom settings that the cache API supports.
public
cacheSettings(array<string|int, mixed> &$config_vars) : void
Parameters
- $config_vars : array<string|int, mixed>
-
Additional config_vars, see ManageSettings.php for usage.
clean()
Empty out the cache in use as best it can
public
final static clean([string $type = '' ]) : void
It may only remove the files of a certain type (if the $type parameter is given) Type can be user, data or left blank - user clears out user data
- data clears out system / opcode data
- If no type is specified will perform a complete cache clearing For cache engines that do not distinguish on types, a full cache flush will be done
Parameters
- $type : string = ''
-
The cache type ('memcached', 'zend' or something else for SMF's file cache)
cleanCache()
Clean out the cache.
public
cleanCache([string $type = '' ]) : bool
Parameters
- $type : string = ''
-
If supported, the type of cache to clear, blank/data or user.
Return values
bool —Whether or not we could clean the cache.
connect()
Connects to the cache method. This defines our $key. If this fails, we return false, otherwise we return true.
public
connect() : bool
Return values
bool —Whether or not the cache method was connected to.
detect()
Get the installed Cache API implementations.
public
final static detect() : array<string|int, mixed>
Return values
array<string|int, mixed>exportStatic()
Provides a way to export a class's public static properties and methods to global namespace.
public
static exportStatic() : void
To do so:
- Use this trait in the class.
- At the END of the class's file, call its exportStatic() method.
Although it might not seem that way at first glance, this approach conforms to section 2.3 of PSR 1, since executing this method is simply a dynamic means of declaring functions when the file is included; it has no other side effects.
Regarding the $backcompat items:
A class's static properties are not exported to global variables unless explicitly included in $backcompat['prop_names'].
$backcompat['prop_names'] is a simple array where the keys are the names of one or more of a class's static properties, and the values are the names of global variables. In each case, the global variable will be set to a reference to the static property. Static properties that are not named in this array will not be exported.
Adding non-static properties to the $backcompat arrays will produce runtime errors. It is the responsibility of the developer to make sure not to do this.
get()
Gets the value from the cache specified by key, so long as it is not older than ttl seconds.
public
final static get(string $key[, int $ttl = 120 ]) : array<string|int, mixed>|null
- It may often "miss", so shouldn't be depended on.
- It supports the same as self::put().
Parameters
- $key : string
-
The key for the value to retrieve
- $ttl : int = 120
-
The maximum age of the cached data
Return values
array<string|int, mixed>|null —The cached data or null if nothing was loaded
getCompatibleVersion()
Gets the latest version of SMF this is compatible with.
public
getCompatibleVersion() : string
Return values
string —the value of $key.
getData()
Retrieves an item from the cache.
public
getData(string $key[, int|null $ttl = null ]) : mixed
Parameters
- $key : string
-
The key to use, the prefix is applied to the key name.
- $ttl : int|null = null
-
Overrides the default TTL. Not really used anymore, but is kept for backwards compatibility.
Return values
mixed —The result from the cache, if there is no data or it is invalid, we return null.
getDefaultTTL()
Gets the TTL as defined from set or the default.
public
getDefaultTTL() : int
Return values
int —the value of $ttl.
getImplementationClassKeyName()
Gets the class identifier of the current caching API implementation.
public
getImplementationClassKeyName() : string
Return values
string —the unique identifier for the current class implementation.
getMinimumVersion()
Gets the min version that we support.
public
getMinimumVersion() : string
Return values
string —the value of $key.
getPrefix()
Gets the prefix as defined from set or the default.
public
getPrefix() : string
Return values
string —the value of $key.
getVersion()
Gets the Version of the Caching API.
public
getVersion() : string
Return values
string —the value of $key.
housekeeping()
Run housekeeping of this cache exp. clean up old data or do optimization
public
housekeeping() : void
invalidateCache()
Invalidate all cached data.
public
invalidateCache() : bool
Return values
bool —Whether or not we could invalidate the cache.
isSupported()
Checks whether we can use the cache method performed by this API.
public
isSupported([bool $test = false ]) : bool
Parameters
- $test : bool = false
-
Test if this is supported or enabled.
Return values
bool —Whether or not the cache is supported
load()
Try to load up a supported caching method.
public
final static load([string $overrideCache = '' ][, bool $fallbackSMF = true ]) : object|false
This is saved in $loadedApi if we are not overriding it.
Parameters
- $overrideCache : string = ''
-
Allows manually specifying a cache accelerator engine.
- $fallbackSMF : bool = true
-
Use the default SMF method if the accelerator fails.
Tags
Return values
object|false —An instance of a child class of this class, or false on failure.
put()
Puts value in the cache under key for ttl seconds.
public
final static put(string $key, mixed $value[, int $ttl = 120 ]) : void
- It may "miss" so shouldn't be depended on
- Uses the cache engine chosen in the ACP and saved in Settings.php
- It supports: memcache: https://php.net/memcache APCu: https://php.net/book.apcu Zend: http://files.zend.com/help/Zend-Platform/output_cache_functions.htm Zend: http://files.zend.com/help/Zend-Platform/zend_cache_functions.htm
Parameters
- $key : string
-
A key for this value
- $value : mixed
-
The data to cache
- $ttl : int = 120
-
How long (in seconds) the data should be cached for
putData()
Stores a value, regardless of whether or not the key already exists (in which case it will overwrite the existing value for that key).
public
putData(string $key, mixed $value[, int|null $ttl = null ]) : mixed
Parameters
- $key : string
-
The key to use, the prefix is applied to the key name.
- $value : mixed
-
The data we wish to save. Use null to delete.
- $ttl : int|null = null
-
How long (in seconds) the data should be cached for. The default TTL will be used if this is null.
Return values
mixed —Whether or not we could save this to the cache.
quickGet()
Try to retrieve a cache entry. On failure, call the appropriate function.
public
final static quickGet(string $key, string $file, string|array<string|int, mixed> $function, array<string|int, mixed> $params[, int $level = 1 ]) : string
Parameters
- $key : string
-
The key for this entry
- $file : string
-
The file associated with this entry
- $function : string|array<string|int, mixed>
-
The function to call
- $params : array<string|int, mixed>
-
Parameters to be passed to the specified function
- $level : int = 1
-
The cache level
Return values
string —The cached data
quit()
Closes connections to the cache method.
public
quit() : bool
Return values
bool —Whether the connections were closed.
setDefaultTTL()
Sets a default Time To Live, if this isn't specified we let the class define it.
public
setDefaultTTL([int $ttl = 120 ]) : bool
Parameters
- $ttl : int = 120
-
The default TTL
Return values
bool —If this was successful or not.
setPrefix()
Sets the cache prefix.
public
setPrefix([string $prefix = '' ]) : bool
Parameters
- $prefix : string = ''
-
The prefix to use. If empty, the prefix will be generated automatically.
Return values
bool —If this was successful or not.
createTempTable()
Create the temp table of valid data.
private
createTempTable() : void
deleteTempTable()
Delete the temp table.
private
deleteTempTable() : void
prepareQueries()
Stores a prepared SQL statement, ensuring that it's not done twice.
private
prepareQueries(array<string|int, mixed> $stmtnames, array<string|int, mixed> $queries) : mixed
Parameters
- $stmtnames : array<string|int, mixed>
- $queries : array<string|int, mixed>
retrieveData()
Retrieve the valid data from temp table.
private
retrieveData() : void