Total members 11894 |It is currently Fri Nov 22, 2024 11:29 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





In order to have higher performance php web application you can use caching modules, in this article we talk about open source caching module named “Memcached” with php. We will discuss how to install memcached in PHP.

After downloading the memcached package, you can run following command:
sudo apt-get install php5-memcached
We assume that you are using PHP5; when dealing with memcached extension you will be dealing with user friendly OOP interface, check the code below:
Code:
$memoryCacheObj = new Memcached();

// assuming you have a memcached daemon running in the local system
// port defaults to 11211

$ memoryCacheObj ->addServer("localhost", 11211);

// cache value
$ memoryCacheObj ->set("username", "codemiles");

// get the cached value
print $memc->get("username");
 

Here is another example with more usage :
Code:

$memc 
= new Memcached();

$memc->addServer("10.20.1.2", 11211);
$memc->addServer("10.20.1.3", 11211);

// set a value & specify the data to expire in 7 minutes
$memc->set("username", "codemiles", 7 * 60);

// replace the value of the existing key, but not modifying the expiry time
$memc->replace("username", "codemiles");

// append data to an existing value
print $memc->append("username","as");

 

// set multi-able elements 
$data = array(
    'username' => 'codemiles',
    'city' => 'newyork',
    'age' => '33');

// set the multi elements array and set to expire 7 minutes from now
$memc->setMulti($data, time() + 7 * 60);

// get multiple elements
$dataCached = $memc->getMulti(array('username', 'city', 'age')); 

You can also save the session data using Memcached solution and this will make the handling faster, you can change it from php.ini:
Code:
Code:
session.save_handler = "memcached"
session.save_path = "hostname:port"


The session key names are prefixed with memc.sess.key.

You can download it from here :
Code:
http://pecl.php.net/package/memcached




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : using cache extension with php
 Solution to AJax Cache problem with Internet Explorer     -  
 Get filename full path,extension and filename     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com