2014-08-16 00:26:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base Local cache object, which borrows the concept from Flow user listener
|
|
|
|
*/
|
|
|
|
abstract class EchoLocalCache {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Max number of objects to hold in $targets. In theory, 1000
|
|
|
|
* is very hard to reach in a normal web request. We need to
|
|
|
|
* put cap so it doesn't reach memory limit when running email
|
|
|
|
* digest against large amount of notications
|
|
|
|
*/
|
|
|
|
const TARGET_MAX_NUM = 1000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Target object cache
|
2018-08-15 19:30:45 +00:00
|
|
|
* @var MapCacheLRU
|
2014-08-16 00:26:42 +00:00
|
|
|
*/
|
|
|
|
protected $targets;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup ids that have not been resolved for a target
|
2018-04-04 15:11:39 +00:00
|
|
|
* @var int[]
|
2014-08-16 00:26:42 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $lookups = [];
|
2014-08-16 00:26:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve ids in lookups to targets
|
|
|
|
*/
|
|
|
|
abstract protected function resolve();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use Factory method like EchoTitleLocalCache::create()
|
|
|
|
*/
|
|
|
|
protected function __construct() {
|
2018-08-15 19:30:45 +00:00
|
|
|
$this->targets = new MapCacheLRU( self::TARGET_MAX_NUM );
|
2014-08-16 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a key to the lookup and the key is used to resolve cache target
|
|
|
|
*
|
|
|
|
* @param int $key
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param null $target
|
2014-08-16 00:26:42 +00:00
|
|
|
*/
|
|
|
|
public function add( $key, $target = null ) {
|
|
|
|
if (
|
|
|
|
count( $this->lookups ) < self::TARGET_MAX_NUM
|
|
|
|
&& !$this->targets->get( $key )
|
|
|
|
) {
|
|
|
|
$this->lookups[$key] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cache target based on the key
|
|
|
|
*
|
|
|
|
* @param int $key
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function get( $key ) {
|
|
|
|
$target = $this->targets->get( $key );
|
|
|
|
if ( $target ) {
|
|
|
|
return $target;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $this->lookups[$key] ) ) {
|
|
|
|
$this->resolve();
|
|
|
|
$target = $this->targets->get( $key );
|
|
|
|
if ( $target ) {
|
|
|
|
return $target;
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-16 00:26:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear everything in local cache
|
|
|
|
*/
|
|
|
|
public function clearAll() {
|
|
|
|
$this->targets->clear();
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->lookups = [];
|
2014-08-16 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getLookups() {
|
|
|
|
return $this->lookups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-15 19:30:45 +00:00
|
|
|
* Get the process cache for testing
|
|
|
|
*
|
|
|
|
* @return MapCacheLRU
|
2014-08-16 00:26:42 +00:00
|
|
|
*/
|
|
|
|
public function getTargets() {
|
|
|
|
return $this->targets;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|