I’m using codeigniter (MVC) and extjs for an application.
The application contains many extjs grids with user related data. I want to cache the server json response up to particular time.
i got an idea to implement this as follows
if(cache file exists and not expired)
{
$data = read_file(cache file path);// the cache file contains json encoded data with .json extention
}
else
{
$result = call to a model function; // returns data from a mysql query
$data = json_encode( $result );
//creating cache file with retrived data
wite_file(cache file path, $data)
}
echo $data; // $data is in json format
i want to implement caching this way. Is it the correct way?.
Totally i want to implement a custom json caching library for caching server json data for extjs grids
Is there any json caching libraries already available for codeigniter.
Please help with your suggestions
Codeigniter has almost exactly what you describe built in. They refer to it as the Caching driver: http://codeigniter.com/user_guide/libraries/caching.html
In your case you would want to initiate it like this:
It will write the cache data to file.
The down side in your case is that CI serializes the data in the cache file as opposed to using JSON. I suppose you could override the driver using json_encode() instead of serialize(). The method above would waste a little bit of resources in that it is both serializing AND using json encoding on output.