You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
9
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapter.php
Executable file
9
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapter.php
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
interface LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf);
|
||||
public function add($key, $value, $ttl = 0, $tableName, $connectionResource);
|
||||
public function del($key, $tableName, $connectionResource);
|
||||
public function get($key, $tableName, $connectionResource);
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource);
|
||||
}
|
40
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterApc.php
Executable file
40
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterApc.php
Executable file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
class LtCacheAdapterApc implements LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
return apc_add($this->getRealKey($tableName, $key), $value, $ttl);
|
||||
}
|
||||
|
||||
public function del($key, $tableName, $connectionResource)
|
||||
{
|
||||
return apc_delete($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function get($key, $tableName, $connectionResource)
|
||||
{
|
||||
return apc_fetch($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
if ($this->del($key, $tableName, $connectionResource))
|
||||
{
|
||||
return $this->add($key, $value, $ttl, $tableName, $connectionResource);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getRealKey($tableName, $key)
|
||||
{
|
||||
return $tableName . "-" . $key;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
class LtCacheAdapterEAccelerator implements LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl=0, $tableName, $connectionResource)
|
||||
{
|
||||
$value = serialize($value); //eAccelerator doesn't serialize object
|
||||
return eaccelerator_put($this->getRealKey($tableName, $key), $value, $ttl);
|
||||
}
|
||||
|
||||
public function del($key, $tableName, $connectionResource)
|
||||
{
|
||||
return eaccelerator_rm($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function get($key, $tableName, $connectionResource)
|
||||
{
|
||||
$value = eaccelerator_get($this->getRealKey($tableName, $key));
|
||||
if (!empty($value))
|
||||
{
|
||||
return unserialize($value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
if ($this->del($key, $tableName, $connectionResource))
|
||||
{
|
||||
return $this->add($key, $value, $ttl, $tableName, $connectionResource);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getRealKey($tableName, $key)
|
||||
{
|
||||
return $tableName . "-" . $key;
|
||||
}
|
||||
}
|
68
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterFile.php
Executable file
68
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterFile.php
Executable file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
class LtCacheAdapterFile implements LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf)
|
||||
{
|
||||
$fileStore = new LtStoreFile;
|
||||
$fileStore->prefix = 'LtCache-file';
|
||||
$fileStore->useSerialize = true;
|
||||
$fileStore->init();
|
||||
return $fileStore;
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
if (0 != $ttl)
|
||||
{
|
||||
$ttl += time();
|
||||
}
|
||||
if (true == $connectionResource->add($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($this->get($key,$tableName,$connectionResource))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->del($key,$tableName,$connectionResource);
|
||||
return $connectionResource->add($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function del($key, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->del($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function get($key, $tableName, $connectionResource)
|
||||
{
|
||||
$cachedArray = $connectionResource->get($this->getRealKey($tableName, $key));
|
||||
if (is_array($cachedArray) && (0 == $cachedArray["ttl"] || $cachedArray["ttl"] > time()))
|
||||
{
|
||||
return $cachedArray["value"];
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
if (0 != $ttl)
|
||||
{
|
||||
$ttl += time();
|
||||
}
|
||||
return $connectionResource->update($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value));
|
||||
}
|
||||
|
||||
protected function getRealKey($tableName, $key)
|
||||
{
|
||||
return $tableName . "-" . $key;
|
||||
}
|
||||
}
|
33
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterMemcache.php
Executable file
33
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterMemcache.php
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
class LtCacheAdapterMemcache implements LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf)
|
||||
{
|
||||
return memcache_connect($hostConf["host"], $hostConf["port"]);
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->add($this->getRealKey($tableName, $key), $value, false, $ttl);
|
||||
}
|
||||
|
||||
public function del($key, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->delete($this->getRealKey($tableName, $key), 0);
|
||||
}
|
||||
|
||||
public function get($key, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->get($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->replace($this->getRealKey($tableName, $key), $value, false, $ttl);
|
||||
}
|
||||
|
||||
protected function getRealKey($tableName, $key)
|
||||
{
|
||||
return $tableName . "-" . $key;
|
||||
}
|
||||
}
|
35
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterMemcached.php
Executable file
35
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterMemcached.php
Executable file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
class LtCacheAdapterMemcached implements LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf)
|
||||
{
|
||||
$connectionResource = new Memcached();
|
||||
$connectionResource->addServer($hostConf["host"], $hostConf["port"]);
|
||||
return $connectionResource;
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl=0, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->add($this->getRealKey($tableName, $key), $value, $ttl);
|
||||
}
|
||||
|
||||
public function del($key, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->delete($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function get($key, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->get($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->replace($this->getRealKey($tableName, $key), $value, $ttl);
|
||||
}
|
||||
|
||||
protected function getRealKey($tableName, $key)
|
||||
{
|
||||
return $tableName . "-" . $key;
|
||||
}
|
||||
}
|
55
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterPhps.php
Executable file
55
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterPhps.php
Executable file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
class LtCacheAdapterPhps implements LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf)
|
||||
{
|
||||
$fileStore = new LtStoreFile;
|
||||
if (isset($hostConf['host']) && is_string($hostConf['host']))
|
||||
{
|
||||
$fileStore->cacheFileRoot = $hostConf['host'];
|
||||
$fileStore->prefix = 'Ltcache-phps-';
|
||||
$fileStore->init();
|
||||
return $fileStore;
|
||||
}
|
||||
else
|
||||
{
|
||||
trigger_error("Must set [host]");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->add($this->getRealKey($tableName, $key), $this->valueToString($value), $ttl);
|
||||
}
|
||||
|
||||
public function del($key, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->del($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function get($key, $tableName, $connectionResource)
|
||||
{
|
||||
return $this->stringToValue($connectionResource->get($this->getRealKey($tableName, $key)));
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
return $connectionResource->update($this->getRealKey($tableName, $key), $this->valueToString($value), $ttl);
|
||||
}
|
||||
|
||||
protected function getRealKey($tableName, $key)
|
||||
{
|
||||
return $tableName . "-" . $key;
|
||||
}
|
||||
|
||||
protected function valueToString($value)
|
||||
{
|
||||
return serialize($value);
|
||||
}
|
||||
|
||||
protected function stringToValue($str)
|
||||
{
|
||||
return unserialize($str);
|
||||
}
|
||||
}
|
43
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterXcache.php
Executable file
43
extend/app_alipay/lotusphp_runtime/Cache/Adapter/CacheAdapterXcache.php
Executable file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
class LtCacheAdapterXcache implements LtCacheAdapter
|
||||
{
|
||||
public function connect($hostConf)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
return xcache_set($this->getRealKey($tableName, $key), $value, $ttl);
|
||||
}
|
||||
|
||||
public function del($key, $tableName, $connectionResource)
|
||||
{
|
||||
return xcache_unset($this->getRealKey($tableName, $key));
|
||||
}
|
||||
|
||||
public function get($key, $tableName, $connectionResource)
|
||||
{
|
||||
$key = $this->getRealKey($tableName, $key);
|
||||
if (xcache_isset($key))
|
||||
{
|
||||
return xcache_get($key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
|
||||
{
|
||||
$key = $this->getRealKey($tableName, $key);
|
||||
if (xcache_isset($key))
|
||||
{
|
||||
return xcache_set($key, $value, $ttl);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getRealKey($tableName, $key)
|
||||
{
|
||||
return $tableName . "-" . $key;
|
||||
}
|
||||
}
|
76
extend/app_alipay/lotusphp_runtime/Cache/Cache.php
Executable file
76
extend/app_alipay/lotusphp_runtime/Cache/Cache.php
Executable file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
class LtCache
|
||||
{
|
||||
public $configHandle;
|
||||
|
||||
public $group;
|
||||
public $node;
|
||||
|
||||
protected $ch;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (! $this->configHandle instanceof LtConfig)
|
||||
{
|
||||
if (class_exists("LtObjectUtil", false))
|
||||
{
|
||||
$this->configHandle = LtObjectUtil::singleton("LtConfig");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->configHandle = new LtConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->ch = new LtCacheHandle;
|
||||
$this->ch->configHandle = $this->configHandle;
|
||||
$this->ch->init();
|
||||
$this->ch->group = $this->getGroup();
|
||||
$this->ch->node = $this->getNode();
|
||||
}
|
||||
|
||||
public function getTDG($tableName)
|
||||
{
|
||||
$tdg = new LtCacheTableDataGateway;
|
||||
$tdg->tableName = $tableName;
|
||||
$tdg->ch = $this->ch;
|
||||
return $tdg;
|
||||
}
|
||||
|
||||
public function changeNode($node)
|
||||
{
|
||||
$this->node = $node;
|
||||
$this->dbh->node = $node;
|
||||
}
|
||||
|
||||
protected function getGroup()
|
||||
{
|
||||
if ($this->group)
|
||||
{
|
||||
return $this->group;
|
||||
}
|
||||
$servers = $this->configHandle->get("cache.servers");
|
||||
if (1 == count($servers))
|
||||
{
|
||||
return key($servers);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getNode()
|
||||
{
|
||||
if ($this->node)
|
||||
{
|
||||
return $this->node;
|
||||
}
|
||||
$servers = $this->configHandle->get("cache.servers");
|
||||
if (1 == count($servers[$this->getGroup()]))
|
||||
{
|
||||
return key($servers[$this->getGroup()]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
14
extend/app_alipay/lotusphp_runtime/Cache/CacheAdapterFactory.php
Executable file
14
extend/app_alipay/lotusphp_runtime/Cache/CacheAdapterFactory.php
Executable file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
class LtCacheAdapterFactory
|
||||
{
|
||||
public function getConnectionAdapter($adapter)
|
||||
{
|
||||
$adapterClassName = "LtCacheAdapter" . ucfirst($adapter);
|
||||
if(!class_exists($adapterClassName))
|
||||
{
|
||||
trigger_error("Invalid adapter: $adapter");
|
||||
return null;
|
||||
}
|
||||
return new $adapterClassName;
|
||||
}
|
||||
}
|
49
extend/app_alipay/lotusphp_runtime/Cache/CacheConfigBuilder.php
Executable file
49
extend/app_alipay/lotusphp_runtime/Cache/CacheConfigBuilder.php
Executable file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
class LtCacheConfigBuilder
|
||||
{
|
||||
protected $servers = array();
|
||||
|
||||
protected $defaultConfig = array(
|
||||
"adapter" => "phps", //apc,xcach,ea; file, phps; memcached
|
||||
//"prefix" => ""
|
||||
//"host" => "localhost", //some ip, hostname
|
||||
//"port" => 3306,
|
||||
);
|
||||
|
||||
public function addSingleHost($hostConfig)
|
||||
{
|
||||
$this->addHost("group_0", "node_0", "master", $hostConfig);
|
||||
}
|
||||
|
||||
public function addHost($groupId, $nodeId = "node_0", $role = "master", $hostConfig)
|
||||
{
|
||||
if (isset($this->servers[$groupId][$nodeId][$role]))
|
||||
{//以相同role的第一个host为默认配置
|
||||
$ref = $this->servers[$groupId][$nodeId][$role][0];
|
||||
}
|
||||
else if ("slave" == $role && isset($this->servers[$groupId][$nodeId]["master"]))
|
||||
{//slave host以master的第一个host为默认配置
|
||||
$ref = $this->servers[$groupId][$nodeId]["master"][0];
|
||||
}
|
||||
else if (isset($this->servers[$groupId]) && count($this->servers[$groupId]))
|
||||
{//以本group第一个node的master第一个host为默认配置
|
||||
$refNode = key($this->servers[$groupId]);
|
||||
$ref = $this->servers[$groupId][$refNode]["master"][0];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isset($hostConfig["adapter"]))
|
||||
{
|
||||
trigger_error("No db adapter specified");
|
||||
}
|
||||
$ref = $this->defaultConfig;
|
||||
}
|
||||
$conf = array_merge($ref, $hostConfig);
|
||||
$this->servers[$groupId][$nodeId][$role][] = $conf;
|
||||
}
|
||||
|
||||
public function getServers()
|
||||
{
|
||||
return $this->servers;
|
||||
}
|
||||
}
|
52
extend/app_alipay/lotusphp_runtime/Cache/CacheConnectionManager.php
Executable file
52
extend/app_alipay/lotusphp_runtime/Cache/CacheConnectionManager.php
Executable file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
class LtCacheConnectionManager
|
||||
{
|
||||
public $configHandle;
|
||||
protected $connectionAdapter;
|
||||
|
||||
public function getConnection($group, $node, $role)
|
||||
{
|
||||
if ($connection = $this->getNewConnection($group, $node, $role))
|
||||
{
|
||||
return array(
|
||||
"connectionAdapter" => $this->connectionAdapter,
|
||||
"connectionResource" => $connection
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
trigger_error("no cache server can be connected");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getNewConnection($group, $node, $role)
|
||||
{
|
||||
$servers = $this->configHandle->get("cache.servers");
|
||||
$hostTotal = count($servers[$group][$node][$role]);
|
||||
$hostIndexArray = array_keys($servers[$group][$node][$role]);
|
||||
while ($hostTotal)
|
||||
{
|
||||
$hashNumber = substr(microtime(),7,1) % $hostTotal;
|
||||
$hostConfig = $servers[$group][$node][$role][$hostIndexArray[$hashNumber]];
|
||||
$cacheFactory = new LtCacheAdapterFactory;
|
||||
$this->connectionAdapter = $cacheFactory->getConnectionAdapter($hostConfig["adapter"]);
|
||||
if ($connection = $this->connectionAdapter->connect($hostConfig))
|
||||
{
|
||||
return $connection;
|
||||
}
|
||||
else
|
||||
{
|
||||
//trigger_error('connection fail', E_USER_WARNING);
|
||||
//delete the unavailable server
|
||||
for ($i = $hashNumber; $i < $hostTotal - 1; $i ++)
|
||||
{
|
||||
$hostIndexArray[$i] = $hostIndexArray[$i+1];
|
||||
}
|
||||
unset($hostIndexArray[$hostTotal-1]);
|
||||
$hostTotal --;
|
||||
}//end else
|
||||
}//end while
|
||||
return false;
|
||||
}
|
||||
}
|
52
extend/app_alipay/lotusphp_runtime/Cache/CacheHandle.php
Executable file
52
extend/app_alipay/lotusphp_runtime/Cache/CacheHandle.php
Executable file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
class LtCacheHandle
|
||||
{
|
||||
public $configHandle;
|
||||
public $group;
|
||||
public $node;
|
||||
public $role = "master";
|
||||
public $connectionManager;
|
||||
public $connectionResource;
|
||||
protected $connectionAdapter;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->connectionManager = new LtCacheConnectionManager;
|
||||
$this->connectionManager->configHandle =$this->configHandle;
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0, $tableName)
|
||||
{
|
||||
$this->initConnection();
|
||||
return $this->connectionAdapter->add($key, $value, $ttl, $tableName, $this->connectionResource);
|
||||
}
|
||||
|
||||
public function del($key, $tableName)
|
||||
{
|
||||
$this->initConnection();
|
||||
return $this->connectionAdapter->del($key, $tableName, $this->connectionResource);
|
||||
}
|
||||
|
||||
public function get($key, $tableName)
|
||||
{
|
||||
$this->initConnection();
|
||||
return $this->connectionAdapter->get($key, $tableName, $this->connectionResource);
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0, $tableName)
|
||||
{
|
||||
$this->initConnection();
|
||||
return $this->connectionAdapter->update($key, $value, $ttl, $tableName, $this->connectionResource);
|
||||
}
|
||||
|
||||
protected function initConnection()
|
||||
{
|
||||
$connectionInfo = $this->connectionManager->getConnection($this->group, $this->node, $this->role);
|
||||
$this->connectionAdapter = $connectionInfo["connectionAdapter"];
|
||||
$this->connectionResource = $connectionInfo["connectionResource"];
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
class LtCacheTableDataGateway
|
||||
{
|
||||
public $tableName;
|
||||
|
||||
public $ch;
|
||||
|
||||
public function add($key, $value, $ttl = 0)
|
||||
{
|
||||
return $this->ch->add($key, $value, $ttl, $this->tableName);
|
||||
}
|
||||
|
||||
public function del($key)
|
||||
{
|
||||
return $this->ch->del($key, $this->tableName);
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
return $this->ch->get($key, $this->tableName);
|
||||
}
|
||||
|
||||
public function update($key, $value, $ttl = 0)
|
||||
{
|
||||
return $this->ch->update($key, $value, $ttl, $this->tableName);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user