diff options
Diffstat (limited to 'plugins/jetpack/vendor/automattic/jetpack-sync/src/class-lock.php')
-rw-r--r-- | plugins/jetpack/vendor/automattic/jetpack-sync/src/class-lock.php | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-lock.php b/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-lock.php new file mode 100644 index 00000000..84d87bc8 --- /dev/null +++ b/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-lock.php @@ -0,0 +1,65 @@ +<?php +/** + * Lock class. + * + * @package automattic/jetpack-sync + */ + +namespace Automattic\Jetpack\Sync; + +/** + * Lock class + */ +class Lock { + /** + * Prefix of the blog lock transient. + * + * @access public + * + * @var string + */ + const LOCK_PREFIX = 'jp_sync_lock_'; + + /** + * Default Lifetime of the lock. + * + * @access public + * + * @var int + */ + const LOCK_TRANSIENT_EXPIRY = 15; // Seconds. + + /** + * Attempt to lock. + * + * @access public + * + * @param string $name lock name. + * @param int $expiry lock duration in seconds. + * + * @return boolean True if succeeded, false otherwise. + */ + public function attempt( $name, $expiry = self::LOCK_TRANSIENT_EXPIRY ) { + $name = self::LOCK_PREFIX . $name; + $locked_time = get_option( $name ); + if ( $locked_time ) { + if ( microtime( true ) < $locked_time ) { + return false; + } + } + update_option( $name, microtime( true ) + $expiry ); + + return true; + } + + /** + * Remove the lock. + * + * @access public + * + * @param string $name lock name. + */ + public function remove( $name ) { + delete_option( self::LOCK_PREFIX . $name ); + } +} |