【发布时间】:2017-04-15 07:46:32
【问题描述】:
TL;DR:我已经设置了 AWS ElastiCache 并通过 EC2 通过 SSH 连接。但是当我尝试连接我的 Codeigniter 应用程序(在同一个 EC2 实例中)时,它会失败并显示它正在尝试连接:
["localhost:11211"]
为什么?不应该是:
["****.****.sae1.cache.amazonaws.com:11211"]
背景是这样的:
我在我管理的应用程序中使用 Codeigniter 2.1.4 和 PHP 5.5,我想使用 Memcached 在 Elasticache 实例上保存一些数据。 p>
我的应用程序在 Elastic Beanstalk 环境中运行,Elasticache 实例与 EB 的 EC2 实例位于同一安全组中,并且我设置了打开所有 TCP 连接的规则。
Memcached 被激活,我们可以看到正在运行 phpinfo:
memcached support enabled
Version 2.2.0
libmemcached version 1.0.8
SASL support yes
Session support yes
igbinary support yes
json support yes
msgpack support no
当我通过 SSH 连接到这个实例时,我可以使用 telnet 连接到 Elasticache 实例:
#telnet ****.****.sae1.cache.amazonaws.com 11211
Trying xx.xx.xx.xx...
Connected to ****.****.sae1.cache.amazonaws.com.
在我的应用程序端,我已经像这样配置了 application/config/staging/memcached.php:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'default' => array(
'host' => '****.****.sae1.cache.amazonaws.com',
'port' => 11211,
'weight' => 1
)
);
在控制器上:
public function memcached(){
$this->load->driver('cache');
if($this->cache->memcached->is_supported()){
$data = $this->cache->memcached->get('foo');
if (!$data){
echo 'cache miss!<br />';
$data = 'bar';
$this->cache->memcached->save('foo',$data, 60);
}
echo $data;
echo '<pre>';
var_dump($this->cache->memcached->cache_info());
echo '</pre>';
}
}
输出总是说我正在尝试连接到 localhost 而不是 Elasticache,为什么?这是输出:
cache miss!
bar
array(1) {
["localhost:11211"]=>
array(24) {
["pid"]=>
int(-1)
["uptime"]=>
int(0)
["threads"]=>
int(0)
["time"]=>
int(0)
["pointer_size"]=>
int(0)
["rusage_user_seconds"]=>
int(0)
["rusage_user_microseconds"]=>
int(0)
["rusage_system_seconds"]=>
int(0)
["rusage_system_microseconds"]=>
int(0)
["curr_items"]=>
int(0)
["total_items"]=>
int(0)
["limit_maxbytes"]=>
int(0)
["curr_connections"]=>
int(0)
["total_connections"]=>
int(0)
["connection_structures"]=>
int(0)
["bytes"]=>
int(0)
["cmd_get"]=>
int(0)
["cmd_set"]=>
int(0)
["get_hits"]=>
int(0)
["get_misses"]=>
int(0)
["evictions"]=>
int(0)
["bytes_read"]=>
int(0)
["bytes_written"]=>
int(0)
["version"]=>
string(0) ""
}
}
抱歉,伙计们提出了一个冗长的问题。但如果您对此有任何想法,我将不胜感激。
干杯。
【问题讨论】:
-
至少进入这个条件
if($this->cache->memcached->is_supported()){...你重启了apache服务了吗?....有时候当我们使用缓存的时候,我们需要重启apache服务来反映这些变化:) -
谢谢@Hackerman!刚刚尝试过,仍然是相同的输出。干杯
-
你是如何管理你的,嗯,路由,你有没有防火墙规则将所有 http 请求首先重定向到你的站点到你的缓存服务器......我的意思是,你有那部分,对吧?
-
我实际上将控制器中生成并作为 $this->data['item'] 传递给视图的所有数据都保存在 Elasticache 上。因此,如果缓存服务器存储了该值,我的控制器将不需要查询我的数据库。所以没有防火墙或 HTTP 规则。我没听错你的问题吗?
标签: php codeigniter amazon-web-services memcached amazon-elasticache