This question was probably asked before, but I cannot decide yet on what I should use. I'm building a small API Wrapper and there are some methods that could use some caching of the data I get with that method.
The data is in JSON format. So while I was browsing I found a few solutions to store this data. One of them is APC, which seemed pretty neat and more like using localStorage on the client side. But then I found that this is an extension that doesn't come with PHP preinstalled, and for the latest versions of PHP there are some bugs ( the developers don't consider it stable ). I also had a hard time adding it to XAMPP so I can test it locally.
So I left it out and considered other options like creating files on the server. This also seemed like a pretty good idea until I discovered that when I create a folder, the chmod
is 0755
which isn't very convenient. I first thought it was something I did, but then I read a post here saying something about Safe Mode, which doesn't allow me to create folders with 0777
mode. So I was looking for a solution and I found that using FTP would solve it. I tried the FTP thing and it worked just fine, just that I had a hard time implementing a way for the connection to remain opened until it timed out and don't overwrite the connection each time I called the method that connected to FTP and created the folder.
Another solution I heard about is Memcache if I'm not mistaken, but that seemed like an overhead.
So I'm a bit in doubt on what I should use for caching the data. If anyone has some pointers it would be appreciated :)
Whatever you do, don't write your own caching solution. The kind of people that can do that, don't need to ask questions about it on SO.
I have worked on apps with >10,000 requests per second using a combination of APC, Redis, Memcache and Varnish. They are very, very good.
I am using Mac for developing and I create folders from php using something like that:
$oldmask = umask(0);
chmod($structure, 0777);
umask($oldmask);
If you are on Windows maby it's for some windows setting ... I don't have to much windows experience.
You can also can make a cache to MySQL if you have problems creating folders.
Try to create a md5 hash for the result you need to cache and store it with the result and a timestamp to a table cacheTbl in your database.
When you store the cache add to the current time the time you need to cache that data;
Next when you need this data you take a look in the database first to see if there is a valid cache for what you request and if it is you take it from there, if it's not in the cache you generate it and store it for the next request.
It can be easily made by building two simple functions:
SetCache($hash, $content, $seconds);
GetCache($hash);
If you need to cache a method result you call SetCache function after your method generate the data to be cached and set the $hash to something like the method name.
On top of your method you check if GetCache has valid cache and if it has you get the result from there and exit thr method or if there is no cache you execute the rest of the method and store cache for next request.
You later can add a cronjob to clean all the old cache .
If you need memory cache you can take a look to: http://memcached.org/
You can try this one, simple with 2 functions set, get. First, you get your object from Cache. If it null, then you do your queries, API get JSON, XML or whatever you like here, then you set it back to cache with 600 seconds to serve 500, 1000 hay 10,000 visitors etc.
With this cache, you don't need to touch your php.ini or worry about root permission to setup it like memcache or APC.
<?php
include("php_fast_cache.php");
// try to get from Cache first.
$html = phpFastCache::get("keyword,page");
if($html == null) {
$html = Render Your Page || Widget || "Hello World";
phpFastCache::set("keyword,page",$html,600);
}
echo or return your $html;
?>