FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-cache.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2012 Hewlett-Packard Development Company, L.P.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this library; if not, write to the Free Software Foundation, Inc.0
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 ***********************************************************/
18 
50 function ReportCacheGet($CacheKey)
51 {
52  global $PG_CONN;
53  global $UserCacheStat;
54 
55  /* Purge old entries ~ 1/500 of the times this fcn is called */
56  if (rand(1, 500) == 1) {
57  ReportCachePurgeByDate(" now() - interval '365 days'");
58  }
59 
64  if ($UserCacheStat == 0) {
65  $sql = "SELECT cache_on FROM report_cache_user WHERE user_fk='$_SESSION[UserId]';";
66  $result = pg_query($PG_CONN, $sql);
67  DBCheckResult($result, $sql, __FILE__, __LINE__);
68  $row = pg_fetch_assoc($result);
69  pg_free_result($result);
70  if (! empty($row['cache_on']) && ($result['cache_on'] == 'N')) {
71  $UserCacheStat = 2;
72  return; /* cache is off for this user */
73  }
74  }
75 
76  $EscKey = pg_escape_string($CacheKey);
77 
78  // update time last accessed
79  $sql = "UPDATE report_cache SET report_cache_tla = now() WHERE report_cache_key='$EscKey';";
80  $result = pg_query($PG_CONN, $sql);
81  DBCheckResult($result, $sql, __FILE__, __LINE__);
82  pg_free_result($result);
83 
84  // Get the cached data
85  $sql = "SELECT report_cache_value FROM report_cache WHERE report_cache_key='$EscKey';";
86  $result = pg_query($PG_CONN, $sql);
87  DBCheckResult($result, $sql, __FILE__, __LINE__);
88  $row = pg_fetch_assoc($result);
89  $cashedvalue = $row['report_cache_value'];
90  pg_free_result($result);
91  return $cashedvalue;
92 } // ReportCacheGet()
93 
94 
102 function ReportCachePut($CacheKey, $CacheValue)
103 {
104  global $PG_CONN;
105  global $UserCacheStat;
106 
107  /* Check if user has cache turned off
108  * CacheUserStat is set in ReportCacheGet
109  * If it isn't, it is safe to fallback to the default
110  * behavior (cache is on).
111  */
112  if ($UserCacheStat == 2) {
113  return;
114  }
115 
116  $EscKey = pg_escape_string($CacheKey);
117  $EscValue = pg_escape_string($CacheValue);
118 
119  /* Parse the key. If the key is a uri */
120  /* look for upload =>, if not found, look for item => */
121  /* in order to get the upload key */
122  $ParsedURI = array();
123  parse_str($EscKey, $ParsedURI);
124  /* use 'upload= ' to define the upload in the cache key */
125  if (array_key_exists("upload", $ParsedURI)) {
126  $Upload = $ParsedURI['upload'];
127  }
128  if (array_key_exists("item", $ParsedURI)) {
129  $sql = "SELECT upload_fk FROM uploadtree WHERE uploadtree_pk='$ParsedURI[item]';";
130  $result = pg_query($PG_CONN, $sql);
131  DBCheckResult($result, $sql, __FILE__, __LINE__);
132 
133  $row = pg_fetch_assoc($result);
134  $Upload = $row['upload_fk'];
135  pg_free_result($result);
136  }
137  if (empty($Upload)) {
138  $Upload = "Null";
139  }
140 
141  $sql = "INSERT INTO report_cache (report_cache_key, report_cache_value, report_cache_uploadfk)
142  VALUES ('$EscKey', '$EscValue', $Upload);";
143  $result = pg_query($PG_CONN, $sql);
144  DBCheckResult($result, $sql, __FILE__, __LINE__);
145  pg_free_result($result);
146  $PGError = pg_last_error($PG_CONN);
147  /* If duplicate key, do an update, else report the error */
148  if (strpos($PGError, "uplicate") > 0) {
149  $sql = "UPDATE report_cache SET report_cache_value = '$EscValue', " .
150  "report_cache_tla=now() WHERE report_cache_key = '$EscKey';";
151  $result = pg_query($PG_CONN, $sql);
152  DBCheckResult($result, $sql, __FILE__, __LINE__);
153  pg_free_result($result);
154  }
155 } // ReportCacheInit()
156 
161 {
162  global $PG_CONN;
163  $sql = "DELETE FROM report_cache;";
164  $result = pg_query($PG_CONN, $sql);
165  DBCheckResult($result, $sql, __FILE__, __LINE__);
166  pg_free_result($result);
167 } // ReportCachePurgeAll()
168 
175 function ReportCachePurgeByDate($PurgeDate)
176 {
177  global $PG_CONN;
178  $sql = "DELETE FROM report_cache WHERE report_cache_tla < $PurgeDate;";
179  $result = pg_query($PG_CONN, $sql);
180  DBCheckResult($result, $sql, __FILE__, __LINE__);
181  pg_free_result($result);
182 } // ReportCachePurgeByDate()
183 
190 function ReportCachePurgeByUpload($UploadPK)
191 {
192  global $PG_CONN;
193  $sql = "DELETE FROM report_cache WHERE report_cache_uploadfk = $UploadPK;";
194  $result = pg_query($PG_CONN, $sql);
195  DBCheckResult($result, $sql, __FILE__, __LINE__);
196  pg_free_result($result);
197 } // ReportCachePurgeByUpload()
198 
207 function ReportCachePurgeByKey($CacheKey)
208 {
209  global $PG_CONN;
210  $ParsedURI = array();
211  $EscKey = pg_escape_string($CacheKey);
212  parse_str($EscKey, $ParsedURI);
213 
214  $sql = "DELETE FROM report_cache WHERE report_cache_key = '$EscKey';";
215  $result = pg_query($PG_CONN, $sql);
216  DBCheckResult($result, $sql, __FILE__, __LINE__);
217  $Err = pg_last_error($PG_CONN);
218  pg_free_result($result);
219 
220  return $Err;
221 } // ReportCachePurgeByKey()
ReportCachePut($CacheKey, $CacheValue)
This function is used to write a record to the report cache. If the record already exists...
ReportCachePurgeByUpload($UploadPK)
Purge from the report cache records for upload $UploadPK.
ReportCachePurgeByKey($CacheKey)
Purge from the report cache the record with $CacheKey.
ReportCachePurgeByDate($PurgeDate)
Purge from the report cache records that have been accessed previous to $PurgeDate.
ReportCachePurgeAll()
Purge all records from the report cache.
ReportCacheGet($CacheKey)
This function is used by Output() to see if the requested report is in the report cache...
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198