]> git.donarmstrong.com Git - roundcube.git/blob - program/include/cache.inc
Imported Upstream version 0.1~rc1
[roundcube.git] / program / include / cache.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/cache.inc                                             |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Provide access to the application cache                             |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: cache.inc 88 2005-12-03 16:54:12Z roundcube $
19
20 */
21
22
23 function rcube_read_cache($key)
24   {
25   global $DB, $CACHE_KEYS;
26   
27   // query db
28   $sql_result = $DB->query("SELECT cache_id, data
29                             FROM ".get_table_name('cache')."
30                             WHERE  user_id=?
31                             AND    cache_key=?",
32                             $_SESSION['user_id'],
33                             $key);
34
35   // get cached data
36   if ($sql_arr = $DB->fetch_assoc($sql_result))
37     {
38     $data = $sql_arr['data'];
39     $CACHE_KEYS[$key] = $sql_arr['cache_id'];
40     }
41   else
42     $data = FALSE;
43
44   return $data;
45   }
46
47
48 function rcube_write_cache($key, $data, $session_cache=FALSE)
49   {
50   global $DB, $CACHE_KEYS, $sess_id;
51   
52   // check if we already have a cache entry for this key
53   if (!isset($CACHE_KEYS[$key]))
54     {
55     $sql_result = $DB->query("SELECT cache_id
56                               FROM ".get_table_name('cache')."
57                               WHERE  user_id=?
58                               AND    cache_key=?",
59                               $_SESSION['user_id'],
60                               $key);
61                                      
62     if ($sql_arr = $DB->fetch_assoc($sql_result))
63       $CACHE_KEYS[$key] = $sql_arr['cache_id'];
64     else
65       $CACHE_KEYS[$key] = FALSE;
66     }
67
68   // update existing cache record
69   if ($CACHE_KEYS[$key])
70     {
71     $DB->query("UPDATE ".get_table_name('cache')."
72                 SET    created=now(),
73                        data=?
74                 WHERE  user_id=?
75                 AND    cache_key=?",
76                 $data,
77                 $_SESSION['user_id'],
78                 $key);
79     }
80   // add new cache record
81   else
82     {
83     $DB->query("INSERT INTO ".get_table_name('cache')."
84                 (created, user_id, session_id, cache_key, data)
85                 VALUES (now(), ?, ?, ?, ?)",
86                 $_SESSION['user_id'],
87                 $session_cache ? $sess_id : 'NULL',
88                 $key,
89                 $data);
90     }
91   }
92
93
94 function rcube_clear_cache($key)
95   {
96   global $DB;
97
98   $DB->query("DELETE FROM ".get_table_name('cache')."
99               WHERE  user_id=?
100               AND    cache_key=?",
101               $_SESSION['user_id'],
102               $key);
103   }
104
105
106 ?>