简体   繁体   中英

I'm trying to save data in memcached from a php script so that my website can directly use the data

The script works fine and is setting the data, but the website code is unable to use it and is instead setting its own memcached values. My website code is written in codeIgniter framework. I don't know why this is happening.

My script code :-

function getFromMemcached($string) {

    $memcached_library = new Memcached();
    $memcached_library->addServer('localhost', 11211);
    $result = $memcached_library->get(md5($string));
    return $result;
}

 function setInMemcached($string,$result,$TTL = 1800) {
    $memcached_library = new Memcached();
    $memcached_library->addServer('localhost', 11211);
    $memcached_library->set(md5($string),$result, $TTL);
}

/*---------- Function stores complete product page as one function call cache -----------------*/
 function getCachedCompleteProduct($productId,$brand)
{
    $result = array();
    $result = getFromMemcached($productId." product page");


    if(true==empty($result))
    {
       //------- REST CODE storing data in $result------

            setInMemcached($productId." product page",$result,1800);    
    }
   return $result;      
}

Website Code :-

private function getFromMemcached($string) {
    $result = $this->memcached_library->get(md5($string));
    return $result;
}

private function setInMemcached($string,$result,$TTL = 1800) {
    $this->memcached_library->add(md5($string),$result, $TTL);
}

/*---------- Function stores complete product page as one function call cache -----------------*/
public function getCachedCompleteProduct($productId,$brand)
{
    $result = array();
    $result = $this->getFromMemcached($productId." product page");


    if(true==empty($result))
    {
    // ----------- Rest Code storing data in $result

    $this->setInMemcached($productId." product page",$result,1800);     
    }
   return $result;      
}

This is saving data in memcached. I checked by printing inside the if condition and checking the final result

Based on the CodeIgniter docs, you can make use of:

class YourController extends CI_Controller() {
  function __construct() {
    $this->load->driver('cache');
  }

  private function getFromMemcached($key) {

    $result = $this->cache->memcached->get(md5($key));
    return $result;
  }

  private function setInMemcached($key, $value, $TTL = 1800) {
    $this->cache->memcached->save(md5($key), $value, $TTL);
  }

  public function getCachedCompleteProduct($productId,$brand) {
    $result = array();
    $result = $this->getFromMemcached($productId." product page");

    if( empty($result) ) {
      // ----------- Rest Code storing data in $result
      $this->setInMemcached($productId." product page",$result,1800);  
    }
    return $result;      
  }
}

Personally try to avoid 3rd party libraries if it already exists in the core framework. And I have tested this, it's working superbly, so that should fix this for you :)

Just remember to follow the instructions at http://ellislab.com/codeigniter/user-guide/libraries/caching.html#memcached to set the config as needed for the memcache server

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