Memcache

3,516 views

Published on

Memcache

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,516
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
66
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Memcache

  1. 1. Memcache
  2. 2. A high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load
  3. 3. Features <ul><li>Very Fast </li></ul><ul><li>APIs </li></ul><ul><li>Hash Table </li></ul><ul><li>LRU </li></ul><ul><li>High Performance </li></ul><ul><li>Slab Allocator </li></ul>
  4. 4. Disadvantages <ul><li>Lacks Access Control </li></ul><ul><li>Security </li></ul>
  5. 5. 1.Installation #Download the extension module apt-get install php5-memcache # Edit /etc/php5/conf.d/memcache.ini and uncomment the following line by removing the semi-colon extension=memcache.so # Restart apache etc/init.d/apache2 restart
  6. 6. Implementation <ul><li>PHP: </li></ul><ul><li><? </li></ul><ul><li>$memcache = new Memcache; </li></ul><ul><li>$memcache->connect ( 'localhost', 11211 ) or die ( &quot;Could not connect&quot; ) ; </li></ul><ul><li>?> </li></ul>
  7. 7. <ul><li>Example: </li></ul><ul><li>$sql = &quot;select * from pages where page_id=1&quot;; </li></ul><ul><li>$qry = mysql_query ( $sql ) or die ( mysql_error () .&quot; : $sql&quot; ) ; </li></ul><ul><li>$result = mysql_fetch_object ( $qry ) ;  </li></ul><ul><li>$content = $result->content; </li></ul>
  8. 8. <ul><li>$sql = &quot;select * from pages where page_id=1&quot;;  </li></ul><ul><li>$key = md5 ( 'query'.$sql ) ;  </li></ul><ul><li>$result = $memcache->get ( $key ) ;  </li></ul><ul><li>if ( $result == null) {   </li></ul><ul><li>$qry = mysql_query ( $sql ) or die ( mysql_error () .&quot; : $sql&quot; ) ;  </li></ul><ul><li>if ( mysql_num_rows ( $qry ) > 0 ) { </li></ul><ul><li>$result = mysql_fetch_object ( $qry ) ; </li></ul><ul><li>$memcache->set ( $key,$result,0,3600 ) ;  </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  9. 9. Functions <ul><li>Memcache::add </li></ul><ul><li>Memcache::get </li></ul><ul><li>Memcache::connect </li></ul><ul><li>Memcache::delete </li></ul><ul><li>Memcache::addServer </li></ul><ul><li>Memcache::close </li></ul>
  10. 10. Memcache::add <?php $memcache_obj = memcache_connect(&quot;localhost&quot;, 11211); $memcache_obj->add('var_key', 'test variable', false, 30); ?>
  11. 11. Memcache::connect <?php $memcache = new Memcache; $memcache->connect('memcache_host', 11211); ?>
  12. 12. Memcache::delete <?php $memcache_obj = new Memcache; $memcache_obj->connect('memcache_host', 11211); $memcache_obj->delete('key_to_delete', 10); ?>
  13. 13. Memcache::get <?php $memcache_obj = new Memcache; $memcache_obj->connect('memcache_host', 11211); $var = $memcache_obj->get('some_key'); ?>
  14. 14. References: http://in.php.net/memcache http://phpbuilder.com/manual/en/intro.memcache.php http://www.hostingcoupons.org/what-is-memcached/
  15. 15. Thank You…

×