2. Overview
Objective
Learn how to use memcache with ProdigyView.
Requirements
Basic Understanding of Cache
Estimated Time
8 Minutes
3. 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
4. Installing Memcache
In order to use memcache, you must have it installed. Installation
differs on distribution but the basics are this.
Ubuntu
1. apt-get install memcached php5-memcahce
2. pecl install memcache
Centos
1. rpm -Uvh
http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-
5-3.noarch.rpm
2. yum install memcached php-pecl-memcache
Both
1. pecl install memcache
2. Copy ‘memcache.so’ into your php.ini
3. service memcached start
4. Restart apache or nginx
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 to
memcache servers connect to the server to
true.
4. Pass the configuration into the init.
6. Write To Memcache
Writing 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
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. 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
9. Replace
Replace 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
10. Removing Memcache
Removing 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
11. 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
12. Review
1. 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
13. API Reference
For a better understanding of cache in ProdigyView, visit
the api by clicking on the link below.
PVCache
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials
www.prodigyview.com