我使用memecached存储值。我在一个文件中启动和插入键值对时工作正常但是当我这样做时在obj orienter方法中它工作不正常,我认为我在启动服务器时做错了但不确定是什么
cache.php
class cache{
function __construct($id = 'someid')
{
$this->id = $id;
$this->obj = new Memcached($id);
$this->host = 'localhost';
$this->port = '11211';
$this->connect($this->host,$this->port );
}
public function connect($host , $port )
{
$servers = $this->obj->getServerList();
if (is_array($servers)) {
foreach ($servers as $server)
if ($server['host'] == $host and $server['port'] == $port)
return true;
}
return $this->obj->addServer($host, $port);
}
public function add($key, $value)
{
$this->obj->add($key, $value);
}
public function get($key)
{
return $this->obj->get($key);
}
}
而不是我在另一个班级中调用它
abc.php
require 'cache.php';
class abc
{
private $cache;
public function __construct()
{
$this->cache = new cache();
}
public function addval($keyname, $value){
$this->cache->add($keyname, $value);
}
public function getCachedVal($keyname)
{
$this->cache->get($keyname));
}
}
现在在 index.php 我打电话给 abc.php
$a = new abc()
$a->addval('demo','hellothisis');
var_dump($a->getCachedVal('demo'));
但我正在
NULL
我也在这里传递id Memcached($id);
,所以我认为我的旧实例不会被销毁
我也有服务器启动
array(1) { [0]=> array(2) { ["host"]=> string(9) "localhost" ["port"]=> int(11211) } }