Documentation

Usercache
in package

Class for early usage of usercache during stage 2.

After init(), so as of postInit() use the ComponentController instead.

Tags
copyright

Cloudrexx AG

author

Michael Ritter michael.ritter@cloudrexx.com

subpackage

core_usercache

todo

Refactor the code and clean up

Table of Contents

Constants

CACHE_ENGINE_MEMCACHED  = 'memcached'
memcached extension
CACHE_ENGINE_OFF  = 'off'
cache off

Properties

$cacheIncrement  : int
Number added to cache prefix to allow "invalidating" the cache
$codeBaseCacheIncrement  : int
Cache increment for codebase cache
$cx  : Cx
$doctrineCacheEngine  : array<string|int, mixed>
$instances  : arrray
$memcached  : Memcached|null
$userCacheEngine  : string|null

Methods

clear()  : void
Flushes the memcached cache of this instance.
deactivateUserCacheForCurrentRequest()  : void
Deactivates Doctrine cache for this request
delete()  : void
Wrapper to delete an item from cache using the doctrine usercache cache driver
fetch()  : mixed
Wrapper to fetch an item from cache using the doctrine usercache cachedriver
getConfiguration()  : array<string|int, mixed>
Returns the usercache configuration
getDoctrineCacheDriver()  : CacheProvider
Detects the correct doctrine cache driver for the user caching engine in use
getInstance()  : Instance
Returns a matching instance for the given Cx instance
isActive()  : bool
Whether user cache is active
isValidConfiguration()  : bool
Tells whether a server is reachable with the current config
reInit()  : void
Re-initializes the usercache
save()  : void
Wrapper to save an item to cache using the doctrine user usercache cachedriver
__construct()  : mixed
Initializes a new instance and initializes usercache if active
flushCodeBaseCache()  : void
Flush the codebase cache from the memcached instance.
flushUserDataCache()  : void
Flush the user data from the memcached instance.
getCachePrefix()  : string
Retunrns the CachePrefix related to this Domain
getKeyForId()  : string
Get the key to identify the element with $id in the memcached cache.
getMemcached()  : Memcached
getUserCacheActive()  : mixed
getUserCacheEngine()  : mixed
initUserCaching()  : mixed
Initializes usercaching
isValidKey()  : bool
Checks if a string is a valid memcached key according to section 'Keys' in https://github.com/memcached/memcached/blob/master/doc/protocol.txt

Constants

CACHE_ENGINE_MEMCACHED

memcached extension

public mixed CACHE_ENGINE_MEMCACHED = 'memcached'

Properties

$cacheIncrement

Number added to cache prefix to allow "invalidating" the cache

protected int $cacheIncrement = 1

Flushing the Cache is not “possible” in a nice way as https://www.php.net/manual/en/memcached.getallkeys.php is not an atomic operation and https://www.php.net/manual/en/memcached.flush.php would flush the whole server. Therefore we need a way to flush only the entries of a website. We do this using an increment number (which we cache as well). Incrementing this number results in the website no longer “seeing” the old entries and Memcached will drop them once they get too old.

$codeBaseCacheIncrement

Cache increment for codebase cache

protected int $codeBaseCacheIncrement = 1

For explanation see static::$cacheIncrement

$doctrineCacheEngine

protected array<string|int, mixed> $doctrineCacheEngine = []

Contains the reference to the Doctrine cache engine used for the query & result cache as well as the reference to the Doctrine meta-data cache. Both cache engines are of type \Doctrine\Common\Cache\CacheProvider. The Query & result cache engine is insted by key false. The meta-data cache is indexed by key true.

$instances

protected static arrray $instances = array()

List of instances of this class indexed by config folder name

Methods

clear()

Flushes the memcached cache of this instance.

public clear() : void

If component SystemInfo is licensed, then this flushes not only the user-data cache (static::flushUserDataCache(), but also the codebase cache (static::flushCodeBaseCache().

deactivateUserCacheForCurrentRequest()

Deactivates Doctrine cache for this request

public deactivateUserCacheForCurrentRequest() : void

Please note that components that directly use the underlying usercache (like ClassLoader and DataSet) do still use usercache after this.

delete()

Wrapper to delete an item from cache using the doctrine usercache cache driver

public delete(string $id[, bool $fromCodebase = false ]) : void
Parameters
$id : string

Id of the item

$fromCodebase : bool = false

Defaults to false. If set to true data is deleted from codebase-wide cache

fetch()

Wrapper to fetch an item from cache using the doctrine usercache cachedriver

public fetch(string $id[, bool $fromCodebase = false ]) : mixed
Parameters
$id : string

Id of the item

$fromCodebase : bool = false

Defaults to false. If set to true fetch is from codebase-wide cache

Return values
mixed

The item

getConfiguration()

Returns the usercache configuration

public getConfiguration() : array<string|int, mixed>
Return values
array<string|int, mixed>

Array with keys "ip" and "port"

getDoctrineCacheDriver()

Detects the correct doctrine cache driver for the user caching engine in use

public getDoctrineCacheDriver([bool $forMetaData = false ]) : CacheProvider
Parameters
$forMetaData : bool = false

Set to true to fetch the cache driver to be used for meta data caching.

Return values
CacheProvider

The doctrine cache driver object

getInstance()

Returns a matching instance for the given Cx instance

public static getInstance(Cx $cx) : Instance
Parameters
$cx : Cx
Return values
Instance

of this class

isActive()

Whether user cache is active

public isActive() : bool
Return values
bool

True if usercache is active, false otherwise

isValidConfiguration()

Tells whether a server is reachable with the current config

public isValidConfiguration() : bool
Return values
bool

True if server can be reached, false otherwise

reInit()

Re-initializes the usercache

public reInit() : void

This is used to apply the new config if the config was changed.

save()

Wrapper to save an item to cache using the doctrine user usercache cachedriver

public save(string $id, mixed $data[, int $lifeTime = 0 ][, bool $fromCodebase = false ]) : void
Parameters
$id : string

Id of the item

$data : mixed

data of the item

$lifeTime : int = 0

Expiraton time of the item (if it equals zero, the item never expires)

$fromCodebase : bool = false

Defaults to false. If set to true data is saved to codebase-wide cache

__construct()

Initializes a new instance and initializes usercache if active

protected __construct(Cx $cx) : mixed
Parameters
$cx : Cx

flushCodeBaseCache()

Flush the codebase cache from the memcached instance.

protected flushCodeBaseCache() : void

Codebase cache contains:

  • classloader cache
  • component metadata cache
  • metadata of doctrine ORM
Tags
todo

Migrate to Memcached::increment() once the $initial_value is supported by the PECL extension.

flushUserDataCache()

Flush the user data from the memcached instance.

protected flushUserDataCache() : void

User data contains:

  • result & query cache of doctrine ORM
  • cached DataSets
  • custom data
Tags
todo

Migrate to Memcached::increment() once the $initial_value is supported by the PECL extension.

getCachePrefix()

Retunrns the CachePrefix related to this Domain

protected getCachePrefix([bool $withoutIncrement = false ][, bool $fromCodebase = false ]) : string
Parameters
$withoutIncrement : bool = false

(optional) If set to true returns the prefix without the increment

$fromCodebase : bool = false

Defaults to false. If set to true prefix for codebase-wide cache is returned

Tags
global

string $_DBCONFIG

Return values
string

CachePrefix

getKeyForId()

Get the key to identify the element with $id in the memcached cache.

protected getKeyForId(string $id[, bool $fromCodebase = false ]) : string
Parameters
$id : string

The ID of the element in the cache.

$fromCodebase : bool = false

Defaults to false. If set to true key for codebase-wide cache is returned

Return values
string

The key used to identify $id in the memcached cache.

getMemcached()

protected getMemcached() : Memcached
Return values
Memcached

The memcached object

initUserCaching()

Initializes usercaching

protected initUserCaching() : mixed

isValidKey()

Checks if a string is a valid memcached key according to section 'Keys' in https://github.com/memcached/memcached/blob/master/doc/protocol.txt

protected isValidKey(string $key) : bool
Parameters
$key : string

The string to be verified as a valid memcached key.

Return values
bool

Whether $key is a valid memcached key.


        
On this page

Search results