Skip to content

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

Closed

Description

@jjkirkpatrick

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?

Activity

  1. panique commented on Apr 22, 2015

    @panique
    Owner

    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 ?

  2. tankerkiller125 commented on May 5, 2015

    @tankerkiller125

    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.

  3. jjkirkpatrick commented on May 6, 2015

    @jjkirkpatrick
    ContributorAuthor

    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.

  4. tankerkiller125 commented on May 6, 2015

    @tankerkiller125

    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.

  5. jjkirkpatrick commented on May 6, 2015

    @jjkirkpatrick
    ContributorAuthor

    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.

  6. tankerkiller125 commented on May 6, 2015

    @tankerkiller125

    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.

  7. jjkirkpatrick commented on May 6, 2015

    @jjkirkpatrick
    ContributorAuthor

    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();
        }
    }
  8. tankerkiller125 commented on May 6, 2015

    @tankerkiller125

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

  9. jjkirkpatrick commented on May 6, 2015

    @jjkirkpatrick
    ContributorAuthor

    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");
          }
  10. changed the title [-][Feature] Simple Caching system.[/-] [+][Feature discussion] Simple Caching system[/+] on May 16, 2015
  11. changed the title [-][Feature discussion] Simple Caching system[/-] [+][Feature discussion] Simple caching system[/+] on May 16, 2015
  12. panique commented on May 16, 2015

    @panique
    Owner

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

  13. changed the title [-][Feature discussion] Simple caching system[/-] [+][STICKY][Feature discussion] Simple caching system[/+] on May 16, 2015
  14. abmmhasan commented on Jun 3, 2015

    @abmmhasan
    Contributor

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

  15. changed the title [-][STICKY][Feature discussion] Simple caching system[/-] [+][STICKY][Optional feature] Simple caching system[/+] on Jul 9, 2015
  16. panique commented on Oct 11, 2015

    @panique
    Owner

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions