【问题标题】:Codeigniter Memcached not connecting to AWS Elasticache InstanceCodeigniter Memcached 未连接到 AWS Elasticache 实例
【发布时间】:2017-04-15 07:46:32
【问题描述】:

TL;DR:我已经设置了 AWS ElastiCache 并通过 EC2 通过 SSH 连接。但是当我尝试连接我的 Codeigniter 应用程序(在同一个 EC2 实例中)时,它会失败并显示它正在尝试连接:

["localhost:11211"]

为什么?不应该是:

["****.****.sae1.cache.amazonaws.com:11211"]

背景是这样的:

我在我管理的应用程序中使用 Codeigniter 2.1.4PHP 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-&gt;cache-&gt;memcached-&gt;is_supported()){...你重启了apache服务了吗?....有时候当我们使用缓存的时候,我们需要重启apache服务来反映这些变化:)
  • 谢谢@Hackerman!刚刚尝试过,仍然是相同的输出。干杯
  • 你是如何管理你的,嗯,路由,你有没有防火墙规则将所有 http 请求首先重定向到你的站点到你的缓存服务器......我的意思是,你有那部分,对吧?
  • 我实际上将控制器中生成并作为 $this->data['item'] 传递给视图的所有数据都保存在 Elasticache 上。因此,如果缓存服务器存储了该值,我的控制器将不需要查询我的数据库。所以没有防火墙或 HTTP 规则。我没听错你的问题吗?

标签: php codeigniter amazon-web-services memcached amazon-elasticache


【解决方案1】:

尝试修改您的memcached.php 文件,例如:

$config['memcached'] = array(
  'hostname' => '****.****.sae1.cache.amazonaws.com',
  'port' => 11211,
  'weight' => 1
);

在你的 memcached 函数上:

$this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'dummy'));

【讨论】:

  • 老兄。太感谢了。你的配置文件做到了!我什至不必编辑我的函数。干杯!
  • 我很高兴能帮助@grpaiva :)
  • 如何在自动加载文件中做到这一点?
  • @Diego,也许您可​​以创建一个具有所有背景的新问题并将链接粘贴到此处....基本上,当您创建像 memcached.php 这样的自定义配置文件并将其调整到application/config/...为了自动加载它,您需要将此行添加到您的autoload.php 文件$autoload['config'] = array('memcached');
猜你喜欢
  • 2019-09-12
  • 2016-12-21
  • 2015-12-22
  • 1970-01-01
  • 2017-04-08
  • 2018-11-28
  • 2011-12-29
  • 2016-09-15
  • 2018-12-29
相关资源
最近更新 更多