Tuesday, 23 September 2014

Zend Cache Tutorial - Zend Framework 1.12

Zend Cache Tutorial - Zend Framework 1.12

Zend_Cache provides a flexible approach toward caching data, including support for tagging, manipulating, iterating, and removing data.

Question: What is Zend Cache?
It is Inbuilt component of zend framework which is used to speed up the application by using the caching concept.


Question: Why we use Zend Cache?
We use Zend cache for different purpose are following
  • Increase the website Performance.
  • Reduce the load on database.
  • Reduce the load of api, if using in application.
  • To get the result faster.


What is the use of  Zend Cache?
We can speedup our application as well as can reduce the burdon on database and API.
Zend_Cache use the file system to store the cache. We have option in zend cache to use the other caching component like Memcached, Sqlite, ibmemcached,Apc,Xcache and ZendPlatform etc. In this tutorial we will understand the zend_cache with file system.


How to Setup Zend Cache?
Step1: Add Following function Bootstrap
    public function _initCache() {
        $cache = Zend_Cache::factory(
                        'Core', 'File', array(
                    'lifetime' => 3600 * 24 * 7, /*caching time is 7 days*/                            
                    'automatic_serialization' => true
                        ), array('cache_dir' => APPLICATION_PATH . '/cache' /* This is caching folder where caching data will be stored and it must be writable by apache **/
                    )
        );
       
        Zend_Registry::set('Cache', $cache); /* set the cache object in zend_registery so that you can globally access*/
    }
Step2: cache folder must be writeable by PHP
Step3: Now, Just use the zend cache
        /* get the cache object */
        $cache = Zend_Registry::get('Cache');
        /* create a unique cache key */
        $cacheKey = "mydata";
        $result = array();
        if (empty($cacheKey) || ($result = $cache->load($cacheKey)) == false) {
            /*
            Here Process the and store the data in $result variable
            */            
            $cache->save($result, $cacheKey);
        }





What is Tagging in Zend Cache?
Tags are a way to categorize cache records. You can add two OR more type of records in single tag. You can create unlimited tags and also can add unlimited records in single tag.


How to used Tagging in Zend Cache.
        /* get the cache object */
        $cache = Zend_Registry::get('Cache');
        /* create a unique cache key */
        $cacheKey = "mydata";
        $result = array();
        if (empty($cacheKey) || ($result = $cache->load($cacheKey)) == false) {
            /*
            Here Process the and store the data in $result variable
            */            
            $cache->save($result, $cacheKey , array('Tags'));
        }


Can we add multiple Tags for 1 Record?
Yes, We can do.
$cache->save($result, $cacheKey , array('Tag1','Tag2','Tag3')); 


How can we clean one cache?
$cache->remove('idToRemove');


How to clean all records?
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);


How to clean outdated records?
$cache->clean(Zend_Cache::CLEANING_MODE_OLD);


How to clean all records of one/more tags?
$cache->clean(
    Zend_Cache::CLEANING_MODE_MATCHING_TAG,
    array('Tag1', 'Tag2')
);

Monday, 22 September 2014

Memcached interview questions and answers

Memcached interview questions and answers

What is Memcached?
It is component which stored the data temporary for 1 Hour/ 6 Hour/1 Day etc. When we integrate the Memcached with our application, performance of application increased.
OR
Memcached is open source, high-performance distributed memory object used for caching so that execution can be enhanced at nth level.



How Memcached Works?
  • Try to get to get the detail of user, Browser send the request to application.
  • Application call the Memcached for particular user.
  • If Result found in Memcached, Return the result from Memcached.
  • If Result Not found in Not Memcached, Application send the request to database and save the result in memcached.
  • Each Memcached have one unique key.
  • Get/Set the data works on the behalf of key.
  • you can also delete one or more keys.
  • You can also assign tags to one/more keys.



Who is Author of Memcached?
Danga Interactive


In which Language Memcached is Written?
'C' Language


What is offical website of Nginx?
http://www.memcached.org/


When Memcached's initial version launched?
May 22, 2003




What is the Best Usage of Memcached?
  • Easy to install in windows as well as in unix system.
  • it offers API integration for all the major languages like Java, PHP, C/C++, Python, Ruby, Perl etc
  • Improve the performance of web application by caching
  • Reduce the burden of Db server
  • You can delete one/more values.
  • you can update the values of keys.
  • You can use use "Tags" for keys.



How to install Memcached?
Click Here


Memcached Commands
Connecting to Memcached server with telnet Command?
telnet hostName portNumber


How to get the value of key?
get key


How to set the value of Key?
set key 0 900 4
Format: set [key] [flags] [exptim] [bytes] [noreply]


How to add the value in Key?
add key 0 900 4
Format: add [key] [flags] [exptim] [bytes] [noreply]


How to replace the value of Key?
replace key 0 900 4
Format: replace [key] [flags] [exptim] [bytes] [noreply]


How to append the value of Key?
append key 0 900 4
Format: append [key] [flags] [exptim] [bytes] [noreply]


How to prepend the value of Key?
prepend key 0 900 4
Format: prepend [key] [flags] [exptim] [bytes] [noreply]


How to delete the Key?
delete key 


How to show the Stats?
stats


How to get the Version?
Version


How to close the connection?
quit