简体   繁体   中英

Problems using Codeigniter with Memcached

I'm using Codeigniter 2.1.4 with PHP 5.5 in an application I manage. I've been testing Memcached to improve app's performance and it was working fine yesterday.

Today I was planning to deploy it live but after some improvements I noticed that it wasn't working anymore. On production I'll use AWS Elasticache (which is also not working on my staging environment - but the purpose of this question is to get it back running on my localhost, after that I'll worry about Elasticache).

Before everything, on MAMP's php info I see:

memcached support       enabled
Version                 2.2.0
libmemcached version    1.0.18
SASL support            yes
Session support         yes
igbinary support        no
json support            no
msgpack support         no

On codeigniter application/config/development/ I have a file called memcached.php with this content:

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
    'default' => array(
        'host'      => 'localhost',
        'port'      => 11211,
        'weight'    => 1
    )
);

My test is simple. I have a controller with this function:

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>';
    }
}

And the output is (always) this:

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) ""
  }
}

The funny thing is that if I edit memcached's host address in that memcached.php config file it still outputs ["localhost:11211"] .

UPDATE:

I've just created a clean application using fresh downloaded Codeigniter 2.1.4 files and it's all the same.

Cheers!

You should check if the memcached service is running. Your results show a pid (process id) of -1 and an uptime of 0 seconds.

There is an actual memcached service that needs to be running on the server, you can check if it's listening properly by trying telnet to port 11211 on localhost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM