thomascube
2005-09-25 4e17e6c9dbac8991ee8b302cb2581241247dc8bc
commit | author | age
4e17e6 1 <?php
T 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  | All rights reserved.                                                  |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Provide access to the application cache                             |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
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(sprintf("SELECT cache_id, data
29                                     FROM   %s
30                                     WHERE  user_id=%d
31                                     AND    cache_key='%s'",
32                                    get_table_name('cache'),
33                                    $_SESSION['user_id'],
34                                    $key));
35
36   // get cached data
37   if ($sql_arr = $DB->fetch_assoc($sql_result))
38     {
39     $data = $sql_arr['data'];
40     $CACHE_KEYS[$key] = $sql_arr['cache_id'];
41     }
42   else
43     $data = FALSE;
44
45   return $data;
46   }
47   
48   
49 function rcube_write_cache($key, $data, $session_cache=FALSE)
50   {
51   global $DB, $CACHE_KEYS, $sess_id;
52   
53   // check if we already have a cache entry for this key
54   if (!isset($CACHE_KEYS[$key]))
55     {
56     $sql_result = $DB->query(sprintf("SELECT cache_id
57                                       FROM   %s
58                                       WHERE  user_id=%d
59                                       AND    cache_key='%s'",
60                                      get_table_name('cache'),
61                                      $_SESSION['user_id'],
62                                      $key));
63                                      
64     if ($sql_arr = $DB->fetch_assoc($sql_result))
65       $CACHE_KEYS[$key] = $sql_arr['cache_id'];
66     else
67       $CACHE_KEYS[$key] = FALSE;
68     }
69
70   // update existing cache record
71   if ($CACHE_KEYS[$key])
72     {
73     $DB->query(sprintf("UPDATE %s
74                         SET    created=NOW(),
75                                data='%s'
76                         WHERE  user_id=%d
77                         AND    cache_key='%s'",
78                        get_table_name('cache'),
79                        addslashes($data),
80                        $_SESSION['user_id'],
81                        $key));
82     }
83   // add new cache record
84   else
85     {
86     $DB->query(sprintf("INSERT INTO %s
87                         (created, user_id, session_id, cache_key, data)
88                         VALUES (NOW(), %d, %s, '%s', '%s')",
89                        get_table_name('cache'),
90                        $_SESSION['user_id'],
91                        $session_cache ? "'$sess_id'" : 'NULL',
92                        $key,
93                        addslashes($data)));
94     }
95   }
96
97
98
99 function rcube_clear_cache($key)
100   {
101   global $DB;
102
103   $DB->query(sprintf("DELETE FROM %s
104                       WHERE  user_id=%d
105                       AND    cache_key='%s'",
106                      get_table_name('cache'),
107                      $_SESSION['user_id'],
108                      $key));
109   }
110
111
112 ?>