Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

File locking for the Cache API

Copyright: 2012 Sam Hemelryk
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 237 lines (9 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

cachelock_file:: (6 methods):
  __construct()
  lock()
  unlock()
  check_state()
  get_lock_file()
  __destruct()


Class: cachelock_file  - X-Ref

File locking plugin

__construct($name, array $configuration = array()   X-Ref
Initialises the cache lock instance.

param: string $name The name of the cache lock
param: array $configuration

lock($key, $ownerid, $block = false)   X-Ref
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.

param: string $key The key that we want to lock
param: string $ownerid A unique identifier for the owner of this lock. Not used by default.
param: bool $block True if we want the program block further execution until the lock has been acquired.
return: bool

unlock($key, $ownerid, $forceunlock = false)   X-Ref
Releases an acquired lock.

For more details see {@link cache_lock::unlock()}

param: string $key
param: string $ownerid A unique identifier for the owner of this lock. Not used by default.
param: bool $forceunlock If set to true the lock will be removed if it exists regardless of whether or not we own it.
return: bool

check_state($key, $ownerid)   X-Ref
Checks if the given key is locked.

param: string $key
param: string $ownerid

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

param: string $key
return: string

__destruct()   X-Ref
Cleans up the instance what it is no longer needed.