Memcache Tutorial PHP


Published on

Learn how to install memcache and use it with ProdigyView.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Memcache Tutorial PHP

  1. 1. Memcache
  2. 2. OverviewObjectiveLearn how to use memcache with ProdigyView.Requirements Basic Understanding of CacheEstimated Time8 Minutes
  3. 3. Follow Along With Code Example1. Download a copy of the example code at Install the system in an environment you feel comfortable testing in.3. Proceed to examples/system/Memcache.php
  4. 4. Installing MemcacheIn order to use memcache, you must have it installed. Installationdiffers on distribution but the basics are this. Ubuntu 1. apt-get install memcached php5-memcahce 2. pecl install memcache Centos 1. rpm -Uvh 5-3.noarch.rpm 2. yum install memcached php-pecl-memcache Both 1. pecl install memcache 2. Copy ‘’ into your php.ini 3. service memcached start 4. Restart apache or nginx
  5. 5. Cache Server Before we can use memcache, we need to setup our memcache servers. Replace the localhost with the server you have memcache running on. Multiple memcache servers can be added to config array.1. Set the option for 2. Set the host 3. Set the option tomemcache servers connect to the server to true. 4. Pass the configuration into the init.
  6. 6. Write To MemcacheWriting to memcache is relatively easy. All you need thekey/name of the cache, and the data to pass the cache.Writing to memcache accepts objects and arrays also.1. Set the key/name of the cache 2. Pass the data to the cache 3. Read data based on key
  7. 7. Set Expiration When writing to memcache, the default expiration is set in the init. We can override the default expiration by adding it into the options.1.Set the key/name of the cache 2. Set the data 3. Set the expiration 4. Read the cache
  8. 8. Add OnlyAdd Only is an option that will only write the data tomemcache if it does not exist or the cache has expired. Thiscan be useful for ensure that only unique values are writtento cache. 1. Only write to cache has expired or does not exist 2. Read cached value
  9. 9. ReplaceReplace is an option that will only replace the cache if it existand has not expired. Otherwise nothing its written. 1. Write cache only if it exist 2. Read the cached value
  10. 10. Removing MemcacheRemoving a value from memcache is accomplished byusing the key that was used to save it. Call thePVCache::removeCache() method. Delete the memcache using the key
  11. 11. Flush MemcacheFlushing the memcache will expire all the data andsubsequently remove all the expired data. There is no keypassed when flushing data, just set the option of flush totrue. Set the option for flushing the memcache
  12. 12. Review1. Set the memcache servers in the init.2. Use PVCache::writeMemcache to write to memcache3. The option add_only will only write to cache if they key does not exist while the option replace will only write to memcache if the key does exist.4. Use PVCache::readMemcache to read a stored cached value5. Use PVCache::deleteMemcache to delete the memcache associated with a key
  13. 13. API ReferenceFor a better understanding of cache in ProdigyView, visitthe api by clicking on the link below.PVCache More TutorialsFor more tutorials, please visit: