Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
cachelock_file Class Reference

File locking plugin. More...

Inheritance diagram for cachelock_file:
cache_lock_interface

Public Member Functions

 __construct ($name, array $configuration=array())
 Initialises the cache lock instance.
 
 __destruct ()
 Cleans up the instance what it is no longer needed.
 
 check_state ($key, $ownerid)
 Checks if the given key is locked.
 
 lock ($key, $ownerid, $block=false)
 Acquire a lock.
 
 unlock ($key, $ownerid, $forceunlock=false)
 Releases an acquired lock.
 

Protected Member Functions

 get_lock_file ($key)
 Gets the name to use for a lock file.
 

Protected Attributes

int $blockattempts = 100
 The number of attempts to acquire a lock when blocking is required before throwing an exception.
 
string $cachedir
 The absolute directory in which lock files will be created and looked for.
 
array $locks = array()
 An array containing the locks that have been acquired but not released so far.
 
int null $maxlife = null
 The maximum life in seconds for a lock file.
 
string $name
 The name of the cache lock instance.
 

Detailed Description

File locking plugin.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

cachelock_file::__construct ( $name,
array $configuration = array() )

Initialises the cache lock instance.

Parameters
string$nameThe name of the cache lock
array$configuration

Implements cache_lock_interface.

◆ __destruct()

cachelock_file::__destruct ( )

Cleans up the instance what it is no longer needed.

Implements cache_lock_interface.

Member Function Documentation

◆ check_state()

cachelock_file::check_state ( $key,
$ownerid )

Checks if the given key is locked.

Parameters
string$key
string$ownerid

Implements cache_lock_interface.

◆ get_lock_file()

cachelock_file::get_lock_file ( $key)
protected

Gets the name to use for a lock file.

Parameters
string$key
Return values
string

◆ lock()

cachelock_file::lock ( $key,
$ownerid,
$block = false )

Acquire a lock.

If the lock can be acquired: This function will return true.

If the lock cannot be acquired the result of this method is determined by the block param: $block = true (default) The function will block any further execution unti the lock can be acquired. This involves the function attempting to acquire the lock and the sleeping for a period of time. This process will be repeated until the lock is required or until a limit is hit (100 by default) in which case a cache exception will be thrown. $block = false The function will return false immediately.

If a max life has been specified and the lock can not be acquired then the lock file will be checked against this time. In the case that the file exceeds that max time it will be forcefully deleted. Because this can obviously be a dangerous thing it is not used by default. If it is used it should be set high enough that we can be as sure as possible that the executing code has completed.

Parameters
string$keyThe key that we want to lock
string$owneridA unique identifier for the owner of this lock. Not used by default.
bool$blockTrue if we want the program block further execution until the lock has been acquired.
Return values
bool
Exceptions
cache_exceptionIf block is set to true and more than 100 attempts have been made to acquire a lock.

Implements cache_lock_interface.

◆ unlock()

cachelock_file::unlock ( $key,
$ownerid,
$forceunlock = false )

Releases an acquired lock.

For more details see cache_lock::unlock()

Parameters
string$key
string$owneridA unique identifier for the owner of this lock. Not used by default.
bool$forceunlockIf set to true the lock will be removed if it exists regardless of whether or not we own it.
Return values
bool

Implements cache_lock_interface.

Member Data Documentation

◆ $locks

array cachelock_file::$locks = array()
protected

An array containing the locks that have been acquired but not released so far.

Array of key => lock file path

◆ $maxlife

int null cachelock_file::$maxlife = null
protected

The maximum life in seconds for a lock file.

By default null for none.


The documentation for this class was generated from the following file: