osdb_cache_get() and osdb_cache_set() for persistent cache

This commit is contained in:
Gudule Lapointe
2025-03-16 21:57:13 +01:00
parent a0d8dfbfa7
commit e73e3e95e2
4 changed files with 100 additions and 14 deletions
+19 -5
View File
@@ -6,6 +6,8 @@ if( ! defined( 'OSHELPERS' ) ) {
exit;
}
require_once( dirname(__DIR__) . '/includes/functions.php' );
class OpenSim_Grid {
private static $grid_stats;
private $grid_info;
@@ -38,10 +40,12 @@ class OpenSim_Grid {
// global $oshelpers_cache;
$key = __METHOD__ . md5( serialize( func_get_args() ) );
$grids_info = os_cache_get( 'grid_info', array() );
$grids_info = os_cache_get( 'grids_info', array() );
if( isset( $grids_info[$key] ) ) {
error_log( 'Cache hit: ' . $key );
return $grids_info[$key];
$info = $grids_info[$key];
if( ! empty( $info['gridname'] ) ) {
return $grids_info[$key];
}
}
$HomeURI = OpenSim::get_option( 'Hypergrid.HomeURI' );
@@ -53,13 +57,19 @@ class OpenSim_Grid {
}
}
// If gridname not found, only try once to fetch grid info again
$info = os_cache_get( 'grid_info_' . $key, false );
if( $info ) {
return $info;
}
$info = array();
// Fetch live info from grid using $login_uri/get_grid_info and parse xml result in array
// Example xml result:
$xml_url = $grid_uri . '/get_grid_info';
try {
$xml = simplexml_load_file( $xml_url );
$xml = @simplexml_load_file( $xml_url );
} catch( Exception $e ) {
$xml = false;
}
@@ -94,7 +104,11 @@ class OpenSim_Grid {
// $oshelpers_cache[$key] = $info;
$grids_info[$key] = $info;
os_cache_set( 'grid_info', $grids_info );
os_cache_set( 'grids_info', $grids_info );
if( is_array( $info ) && empty( $info['gridname'] ) ) {
$info['gridname'] = opensim_format_tp( $grid_uri, TPLINK_TXT );
}
os_cache_set( 'grid_info_' . $key, $info );
return $info;
}
+3 -3
View File
@@ -494,12 +494,12 @@ class OpenSim {
$id = $string;
try {
$id = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $id );
$id = preg_replace('/[-\s]+/', '-', $id );
$id = @transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $id );
} catch ( Error $e ) {
error_log( 'Error sanitizing slug: ' . $e->getMessage() );
error_log( 'Warning (php-intl missing) ' . $e->getMessage() );
$id = $string;
}
$id = preg_replace('/[-\s]+/', '-', $id );
return $id;
}
+9 -2
View File
@@ -33,8 +33,15 @@ class OSPDO extends PDO {
$trace = debug_backtrace()[0];
$trace = $trace['file'] . ':' . $trace['line'];
$statement = $this->prepare( $query, $options );
$result = $statement->execute( $params );
try {
$statement = $this->prepare( $query, $options );
$result = $statement->execute( $params );
} catch ( PDOException $e ) {
error_log( 'Error ' . $e->getCode() . ' ' . $e->getMessage() . ' query ' . print_r( $query, true ) . ' params ' . print_r( $params, true ) . ' ' . $trace );
return false;
}
// $statement = $this->prepare( $query, $options );
// $result = $statement->execute( $params );
if ( $result ) {
return $statement;
+69 -4
View File
@@ -165,10 +165,10 @@ function ossearch_db_tables( $db ) {
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS oshelpers_cache (
`key` VARCHAR(255) NOT NULL,
`value` LONGBLOB,
`expires_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`key`)
`cache_key` VARCHAR(255) NOT NULL,
`cache_value` LONGBLOB,
`cache_expires` int(10) default 0,
PRIMARY KEY (`cache_key`)
) ENGINE=InnoDB;
"
);
@@ -252,6 +252,71 @@ function ossearch_hostUnregister( $hostname, $port ) {
}
}
function osdb_cache_get( $key, $default = null ) {
global $SearchDB;
if ( ! $SearchDB ) {
return $default;
}
$now = time();
$query = $SearchDB->prepareAndExecute(
'SELECT cache_value FROM oshelpers_cache WHERE cache_key = :key AND (cache_expires IS NULL OR cache_expires = 0 OR cache_expires > :now)',
array(
'key' => $key,
'now' => $now,
),
);
if ( $query ) {
$result = $query->fetchAll( PDO::FETCH_ASSOC );
if( is_array( $result ) && isset( $result[0] ) ) {
$raw = $result[0]['cache_value'];
if ( $raw ) {
$value = json_decode( $raw, true );
if( ! $value ) {
$value = $raw;
}
return $value;
} else if( $result ) {
return $result;
}
}
}
return $default;
}
function osdb_cache_set( $key, $value, $expire_delay = 0 ) {
global $SearchDB;
if ( ! $SearchDB ) {
return false;
}
$now = time();
if( $expire_delay > $now ) {
$expires = $expire_delay;
} else if( ! empty( $expire_delay ) ) {
$expires = $now + $expire_delay;
} else {
$expires = 0;
}
$sql = 'INSERT INTO oshelpers_cache (`cache_key`, `cache_value`, `cache_expires`)
VALUES (:key, :value, :expires)
ON DUPLICATE KEY UPDATE `cache_value` = :value, `cache_expires` = :expires';
$query = $SearchDB->prepareAndExecute(
$sql,
array(
'key' => $key,
'value' => json_encode( $value ),
'expires' => $expires,
),
);
return $query;
}
try {
$SearchDB = new OSPDO( 'mysql:host=' . SEARCH_DB_HOST . ';dbname=' . SEARCH_DB_NAME, SEARCH_DB_USER, SEARCH_DB_PASS );
} catch ( PDOException $e ) {