You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[STICKY][Optional feature] Simple caching system #643
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.
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 ?
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.
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.
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.
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.
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.
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 */publicstaticfunctionadd($key, $value, $minuets = 1)
{
if (self::has($key)) {
returnfalse;
} else {
self::put($key, $value, $minuets);
returntrue;
}
}
/** * Check if value is in Cache * @param $key * @return mixed */publicstaticfunctionhas($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 */publicstaticfunctionput($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 */publicstaticfunctionforever($key, $value)
{
returnself::put($key, $value, 1 * 60 * 60 * 24 * 365 * 25);
}
/** * Get a value from cache and then remove from cache. * @param $key * @return bool */publicstaticfunctionpull($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 */publicstaticfunctionget($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 */publicstaticfunctionforget($key)
{
$cache = phpFastCache();
$cache->delete($key);
}
/** * get information on key, such as time left in cache. * @param $key * @return mixed */publicstaticfunctioncacheInfo($key)
{
$cache = phpFastCache();
return$cache->getInfo($key);
}
/** * remove everything from the cache */publicstaticfunctionclearCache()
{
$cache = phpFastCache();
$cache->clean();
}
}
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!
Activity
panique commentedon 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 commentedon May 5, 2015
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
This code would tell the browser to cache the page for 30 seconds. of course this can be easily changed.
jjkirkpatrick commentedon May 6, 2015
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 commentedon May 6, 2015
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 commentedon May 6, 2015
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 ..
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 commentedon May 6, 2015
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 commentedon May 6, 2015
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
tankerkiller125 commentedon May 6, 2015
Any examples of how one would use that in their Model?
jjkirkpatrick commentedon May 6, 2015
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
[-][Feature] Simple Caching system.[/-][+][Feature discussion] Simple Caching system[/+][-][Feature discussion] Simple Caching system[/-][+][Feature discussion] Simple caching system[/+]panique commentedon May 16, 2015
FYI I've linked to this thread right from the readme, I think it's a very useful discussion here!
[-][Feature discussion] Simple caching system[/-][+][STICKY][Feature discussion] Simple caching system[/+]abmmhasan commentedon Jun 3, 2015
No more watching else, just implement this www.phpfastcache.com
[-][STICKY][Feature discussion] Simple caching system[/-][+][STICKY][Optional feature] Simple caching system[/+]panique commentedon 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!