Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought

Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Author Topic: Best approach for Integrating Cache Mechanism inside PureMVC-PHP  (Read 12537 times)
Sr. Member
Posts: 65

View Profile Email
« on: April 16, 2012, 11:05:09 »


I'm trying to work out a best approach for pulling content from cache before it connects to the Database (Proxy modules), I'm testing few ideas but I'm not sure which one is the best and the right approach.

1) Registering a CacheProxy that will check if the cache file exists and pull it or else register DatabaseProxy and Mediator and handle the request. (but I feel I'm coupling model with view because the cache files are basically view files).

2) Registering a Mediator with view component and view component will check if the cache file exists or else it will request mediator to register a proxy followed by calling a data request function on it, Proxy will send the Notification on data retrieval, and the same mediator will respond to notification and will set the data on the view component (kinda long way).

3) Current status of the system, I'm receiving the score request inside a command, say ScoreCommand and inside I'm registering a ScoreMediator and ScoreProxy and calling a retrieveRecords function on ScoreProxy for which ScoreMediator will respond to its notification, should I do if an else statement within command to check if cache exists before I even register ScoreMediator and ScoreProxy.

Thanks for any input, I look forward to feedback in language/platform independent manner, i.e. not the PHP or any other language way, the PureMVC way to handle Cache Mechanism.

P.S. I'm cautious about registering ScoreProxy, I want to register it only if I needed a connection to the database in case of "CACHE MISS" because it creates a database connection inside it's constructor.

« Last Edit: April 16, 2012, 11:31:18 by saad » Logged
Global Moderator
Hero Member
Posts: 2871

View Profile WWW Email
« Reply #1 on: April 17, 2012, 07:13:51 »

It looks like you're suggesting three approaches to implementing a cache, but they don't really look like alternatives, rather things that interact in some way with a cache.

Also you mention a 'cache file'. Maybe I don't fully understand how it is you're planning to implement the cache or what items are going to be in said cache.

But regardless of all that, I suggest implementing a caching proxy.

When results for a given set of request parameters come back from the database, create a framework Proxy instance that holds the result. Register it with a name that is a concatenation of the proxy name, function name, and all the parameters that were passed in (assuming those same parameters passed to that same function will always return the same result).

Whenever a caching proxy's cached functions are called, have them first check to see if a proxy instance is registered with the proxy name, function name, and parameters. If so, then retrieve it and return its data property, QED. If  not, then fetch the data from the database, cache the result as described above and return it.

If you're doing this on the client side, the data might need to be returned asynchronously by calling the same method that sends the result out by notification if you actually fetch from a service or database.

This approach makes the caching transparent to all other actors of the system. They need not be aware of the caching taking place. And you can turn caching on and off easily in that caching proxy by having it only perform those caching operations if a flag is set.
Sr. Member
Posts: 65

View Profile Email
« Reply #2 on: April 17, 2012, 08:42:40 »

Thanks Cliff - Your answer is all in one...

Your answer further refined my question into two categories, client-side caching and server-side caching, thanks for your insight on client-side caching, I'm gonna use it for my applications for iPhone or Flash or JavaScript and save round-trips to server.

I've been trying different things for server-side caching and worked out a solution and I'm glad to see similarities with your answer. I'm posting my solution for guys who would be interested in PureMVC-PHP (Server-side) based caching and for Cliff for any of his cool comments.

My original context was PHP but I tried to have it a generalized, so in PHP context "cache" is an xml or text or html file having database records and is saved on the server, so if the cache file exists we pull it and save server's time or else we connect to database and retrieve records. Unlike Flash or iPhone applications, since web is stateless so caches and proxies can't be in memory all the time so instead we rely on these files.

1) Within StartupCommand, a CacheProxy is registered (intercepts), and based on the parameters/variables (params become path to the cache.xml), if it finds (aka HIT) it simply echoes the content (with CacheMediator and CacheView) and returns the function before even it executes addSubCommand calls for Proxy and View, this is where we save execution and database fetch time.

2) If Cache doesn't exists (aka MISS), it goes ahead with those addSubCommands, creating model and view, this time View while displaying output to the user sends the output buffer to it's Mediator (through an Interface variable which Mediator set itself inside onRegister function hence exposing only Interface functions, view doesn't know about it's mediator). Mediator then talks to the CacheProxy with the output buffer for the cache file along with params.

3) If a request to insert a new record comes in (handled by a command), meaning the cache becomes dirty and unusable, it calls the function on CacheProxy to delete the cache file and inserts the new record (through a Proxy), the next request for the records will automatically re-create the cache file as explained above in (2) and will be reused as explained in (1).

In ApplicationFacade I've created a flag to set the Caching true or false and above steps check for this flag before execution, hence offering a single point of control for caching. Thanks again Cliff for your insight. I've tried to use and incorporate as much as I can from what I've learned from AS3 and Objective-C port into a stateless system and it has been fun doing a complete full fledged database application in PureMVC along with caching mechanism.

I've chosen PureMVC-PHP port after a long careful thought, I've used CodeIgniter and read about ZendFramework plus others, but ZendFramework is too heavy and processor intensive, and we look for the efficiency and shortest possible server execution time, can't afford heavy processes. CodeIgniter is fast but we want Extra Fastest, CodeIgniter has lot of utilities and functions that comes in the package and at the same time it comes in our way, I wanted just the vanilla core of MVC, and add utilities and libraries in our own way (and only if it's really really required). PureMVC doesn't come in our way, it gives us the decoupled approach between different parts of the system and then allows us to build on top of it on our own.

Thanks to Cliff and Hasan for bringing PHP port to life.
« Last Edit: April 17, 2012, 11:52:06 by saad » Logged
Pages: [1]