memcache tutorial php
DESCRIPTION
Learn how to install memcache and use it with ProdigyView.TRANSCRIPT
Memcache
Overview
Objective
Learn how to use memcache with ProdigyView.
Requirements
Basic Understanding of Cache
Estimated Time
8 Minutes
Follow Along With Code Example
1. Download a copy of the example code at www.prodigyview.com/source.
2. Install the system in an environment you feel comfortable testing in.
3.Proceed to examples/system/Memcache.php
Installing MemcacheIn order to use memcache, you must have it installed. Installation differs on distribution but the basics are this.
Ubuntu1. apt-get install memcached php5-memcahce
2. pecl install memcache
Centos1. rpm -Uvh
http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
2. yum install memcached php-pecl-memcache
Both1. pecl install memcache
2. Copy ‘memcache.so’ into your php.ini
3. service memcached start
4. Restart apache or nginx
Cache ServerBefore 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 memcache servers
2. Set the host 3. Set the option to connect to the server to true.
4. Pass the configuration into the init.
Write To MemcacheWriting to memcache is relatively easy. All you need the key/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
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 3. Set the expiration
4. Read the cache
2. Set the data
Add Only
Add Only is an option that will only write the data to memcache if it does not exist or the cache has expired. This can be useful for ensure that only unique values are written to cache.
1. Only write to cache has expired or does not exist
2. Read cached value
ReplaceReplace is an option that will only replace the cache if it exist and has not expired. Otherwise nothing its written.
1. Write cache only if it exist
2. Read the cached value
Removing MemcacheRemoving a value from memcache is accomplished by using the key that was used to save it. Call the PVCache::removeCache() method.
Delete the memcache using the key
Flush Memcache
Flushing the memcache will expire all the data and subsequently remove all the expired data. There is no key passed when flushing data, just set the option of flush to true.
Set the option for flushing the memcache
Review1. Set the memcache servers in the init.
2. Use PVCache::writeMemcache to write to memcache
3. 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 value
5. Use PVCache::deleteMemcache to delete the memcache associated with a key
API ReferenceFor a better understanding of cache in ProdigyView, visit the api by clicking on the link below.
PVCache
www.prodigyview.com
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials