setLifeTime() 공개 메소드

Set a new life time
public setLifeTime ( integer $newLifeTime )
$newLifeTime integer new life time (in seconds)
예제 #1
0
 /**
  * Stores data into cache
  *
  * @param	string	$id
  * @param	mixed	$data
  * @param	array	(optional) $tags
  * @param	int		(optional) $lifetime
  * @return	bool
  */
 public function put($id, $data, array $tags = null, $lifetime = null)
 {
     if (($lifetime = (int) $lifetime) <= 0) {
         $lifetime = null;
     } elseif ($lifetime <= 2592000) {
         $lifetime += VIVVO_START_TIME - 1;
     } else {
         $lifetime -= 1;
     }
     $this->cache_lite->setLifeTime($lifetime);
     return $this->cache_lite->save($data, md5($id), 'vivvo_cache') === true;
 }
예제 #2
0
function glitre_search($args)
{
    global $config;
    include 'inc.config.php';
    include 'File/MARCXML.php';
    $config = get_config($args['library']);
    // Caching of search results
    require 'Cache/Lite.php';
    // Options for the cache
    $options = array('cacheDir' => $config['base_path'] . 'cache/');
    // Create a Cache_Lite object
    $Cache_Lite = new Cache_Lite($options);
    $cacheresult = 'nocache';
    $records = array();
    $single_record = false;
    if (!empty($args['q'])) {
        // Set default values if these two are empty, otherwise the cache won't work properly
        $args['sort_by'] = $args['sort_by'] ? $args['sort_by'] : $config['default_sort_by'];
        $args['sort_order'] = $args['sort_order'] ? $args['sort_order'] : $config['default_sort_order'];
        // Calculate the cache id for the sorted results
        $sorted_cache_id = 'sorted_' . $args['sort_by'] . '_' . $args['sort_order'] . '_' . $args['library'] . '_' . md5(strtolower($args['q']));
        // Check if the results, sorted in the way we want them, are cached
        if ($records = unserialize($Cache_Lite->get($sorted_cache_id))) {
            // We found what we wanted
            $cacheresult = 'sorted';
        } else {
            // Set an id for the search cache
            $search_cache_id = 'search_' . $args['library'] . '_' . md5(strtolower($args['q']));
            // Check if the raw results are already cached
            $marcxml = '';
            if ($marcxml = $Cache_Lite->get($search_cache_id)) {
                // Found it!
                $cacheresult = 'raw';
            } else {
                // Collect the MARCXML in a string
                if (!empty($config['lib']['sru'])) {
                    // SRU
                    $query = urlencode(massage_input($args['q']));
                    $marcxml = get_sru($query);
                } else {
                    // Z39.50
                    $query = "any=" . massage_input($args['q']);
                    $marcxml = get_z($query);
                }
                if (is_array($marcxml) && $marcxml['error']) {
                    return glitre_format_error($marcxml, $args['format']);
                }
                $Cache_Lite->setLifeTime($config['cache_time_search']);
                $Cache_Lite->save($marcxml, $search_cache_id);
            }
            // Sort the records
            $records = glitre_sort($marcxml, $args['sort_by'], $args['sort_order']);
            $cacheablerecords = array();
            foreach ($records as $record) {
                // Remove the serialized objects
                unset($record['marcobj']);
                $cacheablerecords[] = $record;
            }
            $Cache_Lite->setLifeTime($config['cache_time_sorted']);
            $Cache_Lite->save(serialize($cacheablerecords), $sorted_cache_id);
        }
        // Pick out the ones we actually want
        // Note: Counting of pages starts on 0 (zero), so page=2 is actually the 3rd page of results
        // Which page are we showing?
        $page = $args['page'] ? $args['page'] : 0;
        // How many reords should be displayed on a page?
        // TODO: Parameters should probably be ablo to set this, but with a configurable default and upper limit
        $per_page = $config['lib']['records_per_page'] ? $config['lib']['records_per_page'] : $config['records_per_page'];
        // Get the location of the first record
        $first_record = $page * $per_page;
        // Get the total number of records
        $num_of_records = count($records);
        // Slice out the records that make up the page we are looking for
        $records = array_slice($records, $first_record, $per_page);
        // Calculate the position of the last record
        $last_record = $first_record + count($records);
        // Check the number of records after the slice
        if (count($records) < 1) {
            exit('Error: invalid result-page');
        }
        // Recreate the MARC objects if they are missing (because these records were revived from the cache)
        $simplerecords = array();
        foreach ($records as $record) {
            if (!isset($record['marcobj'])) {
                // Simplify the records to just an array of objects
                $marc = new File_MARCXML($record['marcxml'], File_MARC::SOURCE_STRING);
                $simplerecords[] = $marc->next();
            } else {
                $simplerecords[] = $record['marcobj'];
            }
        }
        $records = $simplerecords;
    } elseif (!empty($args['id'])) {
        // Set an id for the single-record-by-id cache
        $record_cache_id = 'record_' . $args['library'] . '_' . md5(strtolower($args['id']));
        // Check if the record is already cached
        $record = '';
        if ($marcxml = $Cache_Lite->get($record_cache_id)) {
            // Found it!
            $cacheresult = 'record';
        } else {
            // Collect the MARCXML in a string
            if (!empty($config['lib']['sru'])) {
                // SRU
                $query = 'rec.id=' . urlencode($args['id']);
                $marcxml = get_sru($query);
            } else {
                // Z39.50
                $query = 'tnr=' . urlencode($args['id']);
                $marcxml = get_z($query);
            }
            $Cache_Lite->setLifeTime($config['cache_time_record']);
            $Cache_Lite->save($marcxml, $record_cache_id);
        }
        $marc = new File_MARCXML($marcxml, File_MARC::SOURCE_STRING);
        $records[] = $marc->next();
        $single_record = true;
    }
    // A simple log for evaluating the cache strategy
    if ($config['cache_log_file']) {
        $qid = $args['q'] ? $args['q'] : $args['id'];
        $log = date("Y-m-d H:i") . "\t" . $page . "\t" . $args['library'] . "\t" . $qid . "\t" . $cacheresult . "\n";
        $fp = fopen($config['cache_log_file'], 'a');
        if ($fp) {
            fwrite($fp, $log);
            fclose($fp);
        } else {
            exit('Could not open ' . $config['cache_log_file']);
        }
    }
    // The position of the first record needs to be bumped up by one
    $first_record++;
    // Format the records
    return glitre_format($records, $args['format'], $single_record, $num_of_records, $first_record, $last_record, $args['content_type'], $args['loggedin_user']);
}