php cache engines

PHP Cache Engines

This article will explain all the available cache engines for php. PHP Cache engines are used to implement cache mechanism for the dynamic sites developed using scripting and programming languages. Cache mechanism will help to increase site performance.

CakePHP/PHP comes with several cache engines built-in, and provides an easy system to implement our own caching systems. The built-in caching engines are:

FileCache :

  • File cache is a simple cache that uses local files.
  • Not quite fast and doesn’t provide as many features for atomic operations
  • Concurrent access are not great at all, if several processes try to read/write at the same time
  • since disk storage is often quite cheap, storing large objects, or elements that are infrequently written work well in files

ApcCache  :

  • APC cache uses the PHP APC extension.
  • Really fast
  • Able to provide atomic read/write features
  • But you have less space
  • Uses shared memory on the webserver to store objects and it’s local to each server too

Wincache :

  • Wincache uses the Wincache extension.
  • Wincache is similar to APC in features and performance, but optimized for Windows and IIS.

XcacheEngine :

  • Xcache is a PHP extension that provides similar features to APC.

MemcacheEngine :

  • Uses the Memcache extension.
  • Provides a very fast cache system
  • Can be distributed across many servers, and provides atomic operations

MemcachedEngine :

  • Uses the Memcached extension.
  • It also interfaces with memcache but provides better performance.
  • Quite fast (a bit less than APC ; but still pretty fast)
  • Shared between all your servers : each item has to be cached only once, even if you have several webservers.
  • You can have several servers in your memcached cluster (which means virtually no limit in the size of the cache)

RedisEngine  :

  • Uses the phpredis extension.
  • Redis provides a fast and persistent cache system similar to memcached
  • also provides atomic operations

Read more about Memcache here

Using Memcache for php

Using Memcache for PHP

This article will explain the process of using memcache for php dynamic websites. Memcache is used to implement cache mechanism using PHP. There are also many cache engines available for PHP, but using memcache for PHP will provide some additional benefits.

Memcache itself providing procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

Below are the requirements that has to be accomplished before going to explain the process of using memcache for PHP.

1) Memcache service has to be installed and started. For windows operation system read this page and for Ubuntu read this page

2) Memcache php extension is installed and enabled to run with PHP. To know installation process read this page.

Considering both Memcache service and Memcache php extension are started and working fine, here we go with Using Memcache for PHP explanation.

Here’s some sample code from php.net site

<?php
  $memcache = new Memcache;
  $memcache->connect("localhost",11211); # You might need to set "localhost" to "127.0.0.1"
  echo "Server's version: " . $memcache->getVersion() . "<br />\n";
  $tmp_object = new stdClass;
  $tmp_object->str_attr = "test";
  $tmp_object->int_attr = 123;
  $memcache->set("key",$tmp_object,false,10);
  echo "Store data in the cache (data will expire in 10 seconds)<br />\n";
  echo "Data from the cache:<br />\n";
  var_dump($memcache->get("key"));

?>

If you have any errors, it will most likely be because the php extension didn’t run. Check the webserver error log to see if it had problems starting. The most common problem is the version of memcache.dll is not compatible with the version of php you are running.

Installing Memcache PHP Extension

Installing Memcache PHP Extension

Installing Memcache PHP extension article will explain how to install Memcache dll file both in Xampp and Wamp servers.

Follow the below steps for Installing Memcache PHP Extension in Wamp or Xampp

1) Download the memcache php extension zip file form this link

2) Unzip and find the php_memcache.dll file.

3) For Xampp server place the dll file in D:\xampp\php\ext folder. Here considering that Xampp is installed in D Drive.

4) For Wamp server place the dll file in D:\wamp\bin\php\php5.2.9-1\ext folder. Here considering that Wamp is installed in D Drive.

5) Open the php.ini file which is located at D:\xampp\php  or D:\wamp\bin\php\php5.2.9-1\  for Xampp or Wamp servers respectively

6) Find and replace

;extension=php_memcache.dll

with

extension=php_memcache.dll

7) If you cannot find this line simply add the following to the extensions list

extension=php_memcache.dll

8) Restart the server ( Wamp/Xampp)

To check the Installation is success or not

  • Please create a php file (phpinfo.php) some where in the wamp/xampp.
  • Run that file through browser.
  • Search for Memcache in the page .
  • If you can see it, means the installation is success and is working fine.

Note : If you have any errors, it will most likely be because the php extension didn’t run. Check the webserver error log to see if it had problems starting. The most common problem is the version of memcache.dll is not compatible with the version of php you are running.

Read Installation of Memcached service on Windows here

Read Installation of Memcached service on Ubuntu here

Memcached Installation on Windows

Memcached Installation on Windows

Here in this article ‘Memcached Installation on Windows’ we are going to see how to install Memcached on Windows.
As we know Memcached is a Free & open source, high-performance, distributed memory object caching system , generic in nature, but intended for use in speeding up dynamic web applications.

Steps of Memcached installation on windows are given below

Step 1 :

  •  First of all we need to download Memcahced Installation / Service files based on the operation system bits ( Windows 32 bit OR windows 64 bit) .
  •  For Windows 32 Bit operation system, use the win32 binary version. You can download it form the here 
  •  For Windows 64 Bit operation system, use the win64 binary version. You can download it form here OR from here

Step 2 :

  •  Unzip the downloaded zip file to find the file named memcached.exe.
  • Create a folder with name memcached in any drive.Prefer the drive (C Drive) in which the operating system installed.
  •  Copy memcahced.exe file from the unziped folder to the newly created folder ( C:\memcached )

Step 3 :

Install the Memcached service. This can be done in two ways

  1. Right click on memcached.exe file and Run as administrator .
  2. Find the cmd.exe in the system, right hand click on the icon and select Run as administrator

Step 4 :

Open the command line interface and go to the folder where memcached.exe file is available.

C:\memcached>

Run the below commands to install the Memcached service and start it.

C:\memcached>memcached.exe -d install

C:\memcached>memcached.exe -d start

If you dont get any errors it means it’s worked.

If  you face some problem. It’s may shows some error like “The program can’t install because MSVCR71.dll is missing from your computer .“

To solve this issue you need to download the MSVCR71.dll file. You can download it from here. Download the dll and paste it inside your system 32 folder which is in C:\Windows\system32  and again go to the command prompt and follow the Step 4. Now it should not throw error.

 

Installation of Memcached on Ubuntu

Read about memcache and cakephp here

set up memcache engine for cakephp

Set up memcache engine for cakephp

Here in this article we are going to see how to set up Memcache Engine for cakephp. Memcached is a Free & open source, high-performance, distributed memory object caching system , generic in nature, but intended for use in speeding up dynamic web applications.

CakePHP comes with several cache engines built-in, and provides an easy system to implement our own caching systems.By default it provides file based caching mechanism which uses local files to store the cached content and is slowest cache engine, and doesn’t provide as many features for atomic operations.

Since the default file engine is very slow there comes the search of alternative cache engines that cackephp provides. one of the most fast search engine that comes in cakephp is Memcache Engine. Below are the steps to set up Memcache Engine for cakephp.

1) Installing the Memcached in operating system.

2) Installing Memcache module/extension for php.

3) Restarting the Apache server.

4) Making Memcache Engine as default engine for cakephp App.

  • Open the bootstrap file which will find in ‘root_folder/app/config/bootstrap.php’
  • Find and replace the code.
Cache::config('default', array('engine' => 'File'));

With

Cache::config('default', array(
    'engine' => 'Memcache',
    'duration' => 3600, // 
    'probability' => 100,
    'prefix' => Inflector::slug(APP_DIR) . '_',
    'servers' => array(
        '127.0.0.1'
    ),
    'persistent' => true,
    'compress' => false,
));

5) Set  Memcache engine as the main caching engine for the app

  • Open the core file which will find in ‘root_folder/app/config/core.php’
  • Find and replace the code.
$engine = 'File';

With

$engine = 'Memcache';

Read more about memcache here

Memcache Installation in Ubuntu

Memcache Installation in Ubuntu

This article explains about installation of Memcache in Ubuntu systems.

Memcache installation in Ubuntu is required to use chache mechanism in the applications developed using php. Many frames woks and CMS developed using php can use the advantages of Memcache.

Below are the steps used to follow for Memcahe installation in ubuntu system.

Ensure that we will be downloading the latest packages to our server:

sudo apt-get update

Install memcached:

sudo apt-get install memcached php5-memcache


Install php-pear which is the repository which stores memcache:

sudo apt-get install php-pear


Unless you already have a compiler on your server,  download build-essential:

sudo apt-get install build-essential


Use PECL  to install memcache:

sudo pecl install memcache

Respond “yes” if asked whether you would like to enable memcache session handler support.
Finally, add memcache to php.ini

sudo echo “extension=memcache.so” > sudo /etc/php5/apache2/php.ini

checkk it terminal by typing command
–>telnet localhost 11211
add extension in php.ini
–>extension=php_memcache.dll
restart apache
–> sudo /etc/init.d/apache2 start
check in phpinfo.php file memcache enabled