Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[STICKY][Optional feature] Simple caching system #643

Closed
jjkirkpatrick opened this issue Apr 14, 2015 · 12 comments
Closed

[STICKY][Optional feature] Simple caching system #643

jjkirkpatrick opened this issue Apr 14, 2015 · 12 comments

Comments

@jjkirkpatrick
Copy link
Contributor

Hi, What would the possibilities of adding a simple caching system using something like PhpFastCache.
I know this is not strictly to do with the login processes however it is to do with the overall performance and usability.

Thoughts?

@panique
Copy link
Owner

panique commented Apr 22, 2015

He, I think a caching system is totally out of scope of the login focus, and it would make things much more complicated. Do you have an idea for a code-level caching system that is just a few lines ?

@tankerkiller125
Copy link

I think I might have found a really really simple solution that involved browser cache instead of server based cache. You can find the exact details here hwoever the basics would be to add something like this to the public/index.php

 header(
 ‘Expires: ‘.gmdate(‘D, d M Y H:i:s’, time()+30).’GMT’);
 }

This code would tell the browser to cache the page for 30 seconds. of course this can be easily changed.

@jjkirkpatrick
Copy link
Contributor Author

browser caching is great, but it only caches the files, the css for example, if we had caching server side maybe with a package like PhpFastcache you could cache the query results, which speeds up the page as the query does not need to be run. and also reduces the effect of lots of query's hitting the server asking for the same thing.

@tankerkiller125
Copy link

Browser side cache will do the same... The reason that it normally doesn't cache PHP webpages is because PHP adds a no-cache header. However the code I just shared overwrites that and forces the browser to cache the actual webpage as well as the CSS and JS. This means that if the user were to refresh the browser within 30 seconds the web server would never even see the request. I think that Query Cacheing is a little to far out of the scope of this project and should be implemented by the users. I don't think that it should be included by default as it is often difficult to implement and depending on the query may have consequences far worse that's not worth the tiny bit of extra speed.

@jjkirkpatrick
Copy link
Contributor Author

Server caching isn't the same,

Say i have a query that gets news articles, for the front page of a website, and that query is ran to get the articles every time a user visits that page, the content will not change often so 20 you had 5 requests a second for that page, over 60 seconds that's 300 requests for the exact same data, if the first user to hit the page then put it in to the cache, the next 299 request will not even touch the database, so you have taken query's down form 300 a minuet to 1 a minuet. using a libary like Phpfast cache it's really easy to set up caching.

For example ..

public function superCoolFunction()
{
    //Load php FastCache
    $cache = phpFastCache();

    //Check if key is already in Cache 
    if ($cache->isExisting('Articles')) {
        //return it if it is
        return $cache->('Articles');
    } else {
        /*
        *run query and procsessing 
        *return array or any other data type after running query 
        */
        //put the key in to the cache
        $cache->set('Articles', $Array, 60);
        $cache->get('Articles');
    }

no every time we need that data we use the cached copy, and that cahced copy exists for 60 seconds.

this could be extracted out in to a class to give function like
Cache::get($key);
Cache::put($key);

And so on, anyway This was just a thought, wondered what everyone else thought about the idea.

@tankerkiller125
Copy link

I do kinda like it (and I'm in fact working on adding my own implementation to my version of this report) however personally i think that it may be a bit to advanced for this framework.

@jjkirkpatrick
Copy link
Contributor Author

if your using Phpfast cache feel free to use this, may also be usefull for other people too, just abstracts everything in to a Cache class that can then be called with Cache::function, easier to work with

<?php
/**
 * Class Cache
 * Using PHPFastCache to cache data
 */
class Cache
{
    /**
     * Add value to Cache if does not already exist
     * @param $key
     * @param $value
     * @param int $minuets
     * @return bool
     */
    public static function add($key, $value, $minuets = 1)
    {
        if (self::has($key)) {
            return false;
        } else {
            self::put($key, $value, $minuets);
            return true;
        }
    }
    /**
     * Check if value is in Cache
     * @param $key
     * @return mixed
     */
    public static function has($key)
    {
        $cache = phpFastCache();
        return $cache->isExisting($key);
    }
    /**
     * put a data in to cache with a key, and time in minuets
     * @param $key
     * @param $value
     * @param int $minuets
     * @return mixed
     */
    public static function put($key, $value, $minuets = 1)
    {
        $cache = phpFastCache();
        return $cache->set($key, $value, $minuets * 60);
    }
    /**
     * Add a Key to the cache forever(25years)
     * @param $key
     * @param $value
     * @return mixed
     */
    public static function forever($key, $value)
    {
        return self::put($key, $value, 1 * 60 * 60 * 24 * 365 * 25);
    }
    /**
     * Get a value from cache and then remove from cache.
     * @param $key
     * @return bool
     */
    public static function pull($key)
    {
        $value = self::get($key);
        self::Remove($key);
        return $value;
    }
    /**
     * get value out of cache, ability to specify default if key is not in the cache
     * @param $key
     * @param bool $default
     * @return bool
     */
    public static function get($key, $default = False)
    {
        $cache = phpFastCache();
        if ($default !== false && self::has($key) === false) {
            return $default;
        } else {
            return $cache->get("$key");
        }
    }
    /**
     * remove key from cache
     * @param $key
     */
    public static function forget($key)
    {
        $cache = phpFastCache();
        $cache->delete($key);
    }
    /**
     * get information on key, such as time left in cache.
     * @param $key
     * @return mixed
     */
    public static function cacheInfo($key)
    {
        $cache = phpFastCache();
        return $cache->getInfo($key);
    }
    /**
     * remove everything from the cache
     */
    public static function clearCache()
    {
        $cache = phpFastCache();
        $cache->clean();
    }
}

@tankerkiller125
Copy link

Any examples of how one would use that in their Model?

@jjkirkpatrick
Copy link
Contributor Author

It's pretty simple really, just throw that Cache class in to the core directory with the other stuff and then use can use caching eveywhere

public function GetAllProductCodes()
    {
        if (Cache::has("Products")) {
            return Cache::get("Products");
        } else {
            Database connection stuff 
            $sql = "Select stuff from stuff ";
           data manipulation stuff 
            }
            Cache::put("Products", $array, 720);
            return cache::get("Products");
      }

@panique panique changed the title [Feature] Simple Caching system. [Feature discussion] Simple Caching system May 16, 2015
@panique panique changed the title [Feature discussion] Simple Caching system [Feature discussion] Simple caching system May 16, 2015
@panique
Copy link
Owner

panique commented May 16, 2015

FYI I've linked to this thread right from the readme, I think it's a very useful discussion here!

@panique panique changed the title [Feature discussion] Simple caching system [STICKY][Feature discussion] Simple caching system May 16, 2015
@abmmhasan
Copy link
Contributor

No more watching else, just implement this www.phpfastcache.com

@panique panique changed the title [STICKY][Feature discussion] Simple caching system [STICKY][Optional feature] Simple caching system Jul 9, 2015
@panique
Copy link
Owner

panique commented Oct 11, 2015

He, I'll close this ticket for now as it's not a bug or so, but it's for sure linked from the readme, so everybody interesting in a potential caching feature will find this easily!

@panique panique closed this as completed Oct 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants