Hello all!
I am currently implementing caching to my site. I'm using Memcached with a fallback to files.
I wanted to know what is the best practice for caching in terms of validating user input.
I am caching an article, where I get the article slug from the URI.
PHP Code:
$slug = $this->clean($slug);
$content = $cache->get("article_" . $slug);
if(!$content) {
// Get article from DB and recache
if(!$article) error("Bad article");
// cache
$cache->save("article_" . $article->slug, $article->content, 3600);
}
echo $content;
This is just a crude example. I'm assuming it is fine to lookup the cache object based on user input (i.e. the $cache->get("article_" . $slug) line). I can see a static::validateKey in the cache library file that seems to do it's own sanitizing too.
Or should I always be checking to see if the article exists by doing a DB query (kinda defeating the point i guess).
Does this seem correct and valid?