-
Notifications
You must be signed in to change notification settings - Fork 788
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
Comments
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
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. |
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 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
*/
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();
}
} |
Any examples of how one would use that in their Model? |
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");
} |
FYI I've linked to this thread right from the readme, I think it's a very useful discussion here! |
No more watching else, just implement this www.phpfastcache.com |
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! |
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?
The text was updated successfully, but these errors were encountered: