summaryrefslogtreecommitdiff
path: root/program/include/cache.inc
blob: 06e0681ce81cd449461538261d6d8f524e6a1023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

/*
 +-----------------------------------------------------------------------+
 | program/include/cache.inc                                             |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Provide access to the application cache                             |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 +-----------------------------------------------------------------------+

 $Id$

*/


function rcube_read_cache($key)
  {
  global $DB, $CACHE_KEYS;
  
  // query db
  $sql_result = $DB->query("SELECT cache_id, data
                            FROM ".get_table_name('cache')."
                            WHERE  user_id=?
                            AND    cache_key=?",
                            $_SESSION['user_id'],
                            $key);

  // get cached data
  if ($sql_arr = $DB->fetch_assoc($sql_result))
    {
    $data = $sql_arr['data'];
    $CACHE_KEYS[$key] = $sql_arr['cache_id'];
    }
  else
    $data = FALSE;

  return $data;
  }


function rcube_write_cache($key, $data, $session_cache=FALSE)
  {
  global $DB, $CACHE_KEYS, $sess_id;
  
  // check if we already have a cache entry for this key
  if (!isset($CACHE_KEYS[$key]))
    {
    $sql_result = $DB->query("SELECT cache_id
                              FROM ".get_table_name('cache')."
                              WHERE  user_id=?
                              AND    cache_key=?",
                              $_SESSION['user_id'],
                              $key);
                                     
    if ($sql_arr = $DB->fetch_assoc($sql_result))
      $CACHE_KEYS[$key] = $sql_arr['cache_id'];
    else
      $CACHE_KEYS[$key] = FALSE;
    }

  // update existing cache record
  if ($CACHE_KEYS[$key])
    {
    $DB->query("UPDATE ".get_table_name('cache')."
                SET    created=now(),
                       data=?
                WHERE  user_id=?
                AND    cache_key=?",
                $data,
                $_SESSION['user_id'],
                $key);
    }
  // add new cache record
  else
    {
    $DB->query("INSERT INTO ".get_table_name('cache')."
                (created, user_id, session_id, cache_key, data)
                VALUES (now(), ?, ?, ?, ?)",
                $_SESSION['user_id'],
                $session_cache ? $sess_id : 'NULL',
                $key,
                $data);
    }
  }


function rcube_clear_cache($key)
  {
  global $DB;

  $DB->query("DELETE FROM ".get_table_name('cache')."
              WHERE  user_id=?
              AND    cache_key=?",
              $_SESSION['user_id'],
              $key);
  }


?>