0
votes

I am on the way with writing small PHP framework, and now I am facing the "mind" issue. The issue is how to handle the user data which is (logged name, logged status) etc. For now while reading many tutorials I have seen only the simple way, like to cache the queries, full HTML output etc. So what do you think, should I cache only the View? So for example load the header that contain user "not sensitive" data, and the footer, and after all checking if the cookie for specific page exists?

Like if user is on the news page and the cache time is 10 minutes, read the full content (View only) from the cached file?

But how to handle the sessions in cached file, which is simple HTML output, I need to prevent that user may see a cached file of different user for some reason (like m5/sha1 hash that been assigned twice), or to save the cache files per user?

What do you think over all, should I only cache the information that need to integrate with database, for example load all news then cache it in file (that the user data is excluded), then load dynamically from the COOKIE/SESSION (the user information / cached file hash id - then load it) ?

Or there is any simple solution / or not simple to handle this? I am not saying to use any PHP Modules, this question been sent to get answer (the best to get a schema of cache system with user data), then I would develop it for myself (also to gain the experience).

Thanks, and I wish you good day! :)

2

2 Answers

2
votes

Premature optimization is the root of all evil sayeth Donald Knuth, and that certainly applies here. You don't know how to design your caching because you don't know what's slow (indeed there is little here to suggest you know if it's slow).

A cookie is too small to be effective as a cache mechanism and the session is the wrong place to store cache data (although in some circumstances, using the session id as a cache key might be a sensible approach).

1
votes

Caching 'news' pages sounds good, what to cache, depends on what makes your application slow.

Have you profiled your website?, install xdebug and look at what is slow. If the queries are slow, you can cache the query output, but most of the time it will be funtions that 'do something' with the data.

For your question on caching user data, why not make a wrapper function like this:

function getUserByID($pID) {
    $user = apc_fetch("user_".$pID);
    if(empty($user)) {
        $user = CreateUserObject();
        apc_store(serialize($user));
        return $user;
    } else {
        return unserialize($user);
    }
}

The above is just an example using apcu, you can use w/e caching method you want.