1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-23 06:21:12 +01:00
parallel/src/Worker/Environment.php

202 lines
3.8 KiB
PHP
Raw Normal View History

2015-09-09 23:29:41 -05:00
<?php
namespace Icicle\Concurrent\Worker;
use Icicle\Loop;
2015-09-10 18:37:34 -05:00
class Environment implements \ArrayAccess, \Countable
2015-09-09 23:29:41 -05:00
{
/**
* @var array
*/
private $data = [];
2015-09-10 18:37:34 -05:00
/**
* @var array
*/
private $ttl = [];
/**
* @var array
*/
private $expire = [];
2015-09-09 23:29:41 -05:00
/**
* @var \SplPriorityQueue
*/
private $queue;
/**
* @var \Icicle\Loop\Events\TimerInterface
*/
private $timer;
public function __construct()
{
$this->queue = new \SplPriorityQueue();
$this->timer = Loop\periodic(1, function () {
$time = time();
while (!$this->queue->isEmpty()) {
$key = $this->queue->top();
2015-09-10 18:37:34 -05:00
if (isset($this->expire[$key])) {
if ($time <= $this->expire[$key]) {
2015-09-09 23:29:41 -05:00
break;
}
2015-09-10 18:37:34 -05:00
$this->delete($key);
2015-09-09 23:29:41 -05:00
}
$this->queue->extract();
}
if ($this->queue->isEmpty()) {
$this->timer->stop();
}
});
$this->timer->stop();
$this->timer->unreference();
}
/**
* @param string $key
*
* @return bool
*/
public function exists($key)
{
2015-09-10 18:37:34 -05:00
return isset($this->data[(string) $key]);
2015-09-09 23:29:41 -05:00
}
/**
* @param string $key
*
2015-09-10 18:37:34 -05:00
* @return mixed|null Returns null if the key does not exist.
2015-09-09 23:29:41 -05:00
*/
public function get($key)
{
2015-09-10 18:37:34 -05:00
$key = (string) $key;
2015-09-09 23:29:41 -05:00
2015-09-11 00:18:37 -05:00
if (isset($this->ttl[$key]) && 0 !== $this->ttl[$key]) {
2015-09-10 18:37:34 -05:00
$this->expire[$key] = time() + $this->ttl[$key];
$this->queue->insert($key, -$this->expire[$key]);
2015-09-09 23:29:41 -05:00
}
2015-09-10 18:37:34 -05:00
return isset($this->data[$key]) ? $this->data[$key] : null;
2015-09-09 23:29:41 -05:00
}
/**
* @param string $key
2015-09-10 18:37:34 -05:00
* @param mixed $value Using null for the value deletes the key.
2015-09-09 23:29:41 -05:00
* @param int $ttl
*/
public function set($key, $value, $ttl = 0)
{
2015-09-10 18:37:34 -05:00
$key = (string) $key;
if (null === $value) {
$this->delete($key);
return;
}
2015-09-09 23:29:41 -05:00
$ttl = (int) $ttl;
if (0 > $ttl) {
$ttl = 0;
}
if (0 !== $ttl) {
2015-09-10 18:37:34 -05:00
$this->ttl[$key] = $ttl;
$this->expire[$key] = time() + $ttl;
$this->queue->insert($key, -$this->expire[$key]);
2015-09-09 23:29:41 -05:00
if (!$this->timer->isPending()) {
$this->timer->start();
}
2015-09-11 00:18:37 -05:00
} else {
unset($this->expire[$key]);
unset($this->ttl[$key]);
2015-09-09 23:29:41 -05:00
}
2015-09-10 18:37:34 -05:00
$this->data[$key] = $value;
2015-09-09 23:29:41 -05:00
}
/**
* @param string $key
*/
public function delete($key)
{
2015-09-10 18:37:34 -05:00
$key = (string) $key;
2015-09-09 23:29:41 -05:00
unset($this->data[$key]);
2015-09-10 18:37:34 -05:00
unset($this->expire[$key]);
unset($this->ttl[$key]);
}
/**
* Alias of exists().
*
* @param $key
*
* @return bool
*/
public function offsetExists($key)
{
return $this->exists($key);
}
/**
* Alias of get().
*
* @param string $key
*
* @return mixed
*/
public function offsetGet($key)
{
return $this->get($key);
}
/**
* Alias of set() with $ttl = 0.
*
* @param string $key
* @param mixed $value
*/
public function offsetSet($key, $value)
{
$this->set($key, $value);
}
/**
* Alias of delete().
*
* @param string $key
*/
public function offsetUnset($key)
{
$this->delete($key);
2015-09-09 23:29:41 -05:00
}
/**
* @return int
*/
public function count()
{
return count($this->data);
}
2015-09-10 18:37:34 -05:00
/**
* Removes all values.
*/
public function clear()
{
$this->data = [];
$this->expire = [];
$this->ttl = [];
$this->timer->stop();
$this->queue = new \SplPriorityQueue();
}
2015-09-09 23:29:41 -05:00
}