33
27
* database based objects in ampache. It attempts to do some standard
34
28
* caching for all of the objects to cut down on the database calls
37
* @copyright 2001 - 2011 Ampache.org
38
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
39
* @link http://www.ampache.org/
42
31
abstract class database_object {
44
private static $object_cache = array();
46
// Statistics for debugging
47
public static $cache_hit = 0;
48
private static $_enabled = false;
52
* retrieves the info from the database and puts it in the cache
54
public function get_info($id,$table_name='') {
56
$table_name = $table_name ? Dba::escape($table_name) : Dba::escape(strtolower(get_class($this)));
58
// Make sure we've got a real id
59
if (!is_numeric($id)) { return array(); }
61
if (self::is_cached($table_name,$id)) {
62
return self::get_from_cache($table_name,$id);
65
$sql = "SELECT * FROM `$table_name` WHERE `id`='$id'";
66
$db_results = Dba::read($sql);
68
if (!$db_results) { return array(); }
70
$row = Dba::fetch_assoc($db_results);
72
self::add_to_cache($table_name,$id,$row);
81
public static function clear_cache() {
82
self::$object_cache = array();
87
* this checks the cache to see if the specified object is there
89
public static function is_cached($index,$id) {
91
// Make sure we've got some parents here before we dive below
92
if (!isset(self::$object_cache[$index])) { return false; }
94
return isset(self::$object_cache[$index][$id]);
100
* This attempts to retrive the specified object from the cache we've got here
102
public static function get_from_cache($index,$id) {
104
// Check if the object is set
105
if (isset(self::$object_cache[$index])
106
&& isset(self::$object_cache[$index][$id])
110
return self::$object_cache[$index][$id];
119
* This adds the specified object to the specified index in the cache
121
public static function add_to_cache($index,$id,$data) {
123
if (!self::$_enabled) { return false; }
125
$value = is_null($data) ? false : $data;
126
self::$object_cache[$index][$id] = $value;
132
* This function clears something from the cache, there are a few places we need to do this
133
* in order to have things display correctly
135
public static function remove_from_cache($index,$id) {
137
if (isset(self::$object_cache[$index]) && isset(self::$object_cache[$index][$id])) {
138
unset(self::$object_cache[$index][$id]);
141
} // remove_from_cache
145
* Load in the cache settings once so we can avoid function calls
147
public static function _auto_init() {
149
self::$_enabled = Config::get('memory_cache');
33
private static $object_cache = array();
35
// Statistics for debugging
36
public static $cache_hit = 0;
37
private static $_enabled = false;
41
* retrieves the info from the database and puts it in the cache
43
public function get_info($id,$table_name='') {
45
$table_name = $table_name ? Dba::escape($table_name) : Dba::escape(strtolower(get_class($this)));
47
// Make sure we've got a real id
48
if (!is_numeric($id)) { return array(); }
50
if (self::is_cached($table_name,$id)) {
51
return self::get_from_cache($table_name,$id);
54
$sql = "SELECT * FROM `$table_name` WHERE `id`='$id'";
55
$db_results = Dba::read($sql);
57
if (!$db_results) { return array(); }
59
$row = Dba::fetch_assoc($db_results);
61
self::add_to_cache($table_name,$id,$row);
70
public static function clear_cache() {
71
self::$object_cache = array();
76
* this checks the cache to see if the specified object is there
78
public static function is_cached($index,$id) {
80
// Make sure we've got some parents here before we dive below
81
if (!isset(self::$object_cache[$index])) { return false; }
83
return isset(self::$object_cache[$index][$id]);
89
* This attempts to retrive the specified object from the cache we've got here
91
public static function get_from_cache($index,$id) {
93
// Check if the object is set
94
if (isset(self::$object_cache[$index])
95
&& isset(self::$object_cache[$index][$id])
99
return self::$object_cache[$index][$id];
108
* This adds the specified object to the specified index in the cache
110
public static function add_to_cache($index,$id,$data) {
112
if (!self::$_enabled) { return false; }
114
$value = is_null($data) ? false : $data;
115
self::$object_cache[$index][$id] = $value;
121
* This function clears something from the cache, there are a few places we need to do this
122
* in order to have things display correctly
124
public static function remove_from_cache($index,$id) {
126
if (isset(self::$object_cache[$index]) && isset(self::$object_cache[$index][$id])) {
127
unset(self::$object_cache[$index][$id]);
130
} // remove_from_cache
134
* Load in the cache settings once so we can avoid function calls
136
public static function _auto_init() {
138
self::$_enabled = Config::get('memory_cache');
153
142
} // end database_object