summaryrefslogtreecommitdiffstats
path: root/application
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2015-04-08 01:05:46 +0200
committerFlorian Pritz <bluewind@xinu.at>2015-04-08 01:05:46 +0200
commit814c2b44fcc500d2f8f2acb7c9443079908ddaa2 (patch)
tree173d0dbe656bfbc654583e4556c2bbe0482da49d /application
parentd191251243863015f70f758d95c2dd02a9270484 (diff)
Cache filedata
This brings down render time of a multipaste with 180 items from ~180ms to ~80ms. Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application')
-rw-r--r--application/helpers/filebin_helper.php19
-rw-r--r--application/models/mfile.php27
2 files changed, 33 insertions, 13 deletions
diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php
index b5df4f877..7fd25d5f4 100644
--- a/application/helpers/filebin_helper.php
+++ b/application/helpers/filebin_helper.php
@@ -282,6 +282,23 @@ function stateful_client()
return true;
}
+function init_cache()
+{
+ static $done = false;
+ if ($done) {return;}
+
+ $CI =& get_instance();
+ $CI->load->driver('cache', array('adapter' => $CI->config->item("cache_backend")));
+ $done = true;
+}
+
+function delete_cache($key)
+{
+ init_cache();
+ $CI =& get_instance();
+ $CI->cache->delete($key);
+}
+
/**
* Cache the result of the function call in the cache backend.
* @param key cache key to use
@@ -291,8 +308,8 @@ function stateful_client()
*/
function cache_function($key, $ttl, $function)
{
+ init_cache();
$CI =& get_instance();
- $CI->load->driver('cache', array('adapter' => $CI->config->item("cache_backend")));
if (! $content = $CI->cache->get($key)) {
$content = $function();
$CI->cache->save($key, $content, $ttl);
diff --git a/application/models/mfile.php b/application/models/mfile.php
index 724994f83..9af0ed497 100644
--- a/application/models/mfile.php
+++ b/application/models/mfile.php
@@ -69,18 +69,20 @@ class Mfile extends CI_Model {
function get_filedata($id)
{
- $query = $this->db
- ->select('id, hash, filename, mimetype, date, user, filesize')
- ->from('files')
- ->where('id', $id)
- ->limit(1)
- ->get();
-
- if ($query->num_rows() > 0) {
- return $query->row_array();
- } else {
- return false;
- }
+ return cache_function("filedata-$id", 300, function() use ($id) {
+ $query = $this->db
+ ->select('id, hash, filename, mimetype, date, user, filesize')
+ ->from('files')
+ ->where('id', $id)
+ ->limit(1)
+ ->get();
+
+ if ($query->num_rows() > 0) {
+ return $query->row_array();
+ } else {
+ return false;
+ }
+ });
}
// return the folder in which the file with $hash is stored
@@ -214,6 +216,7 @@ class Mfile extends CI_Model {
$this->db->where('id', $id)
->delete('files');
+ delete_cache("filedata-$id");
foreach ($map as $entry) {
assert(!empty($entry['url_id']));