<commit_before><?php
namespace Gaia\Souk;
use Gaia\Store;
use Gaia\DB\Transaction;

/**
* This class is a wrapper class around Souk that allows us to cache the results in a write-thru
* approach. 
*/
class Cacher extends Passthru {
   
   /**
    * how long to cache the data
    */
    const CACHE_TIMEOUT = 86400;
    
    /**
    * cache searches differently because the cache becomes dirty quickly. 
    */
    const SEARCH_CACHE_TIMEOUT = 600;
    
    /**
    * cache prefix key
    */
    const SEARCH_VECTOR_PREFIX =  'sv01/';
    
    /**
    * refresh the cache, when reading?
    * @see self::forceRefresh
    */
    protected $refresh = FALSE;
    
    protected $cacher;
    
    public function __construct( Iface $core, Store\Iface $cacher ){
        parent::__construct( $core );
        $this->cacher = $cacher;
    }
    
    /**
    * perform the auction and store the results in the cache.
    */
    public function auction( $l, array $data = NULL ){
        return $this->writeToCache( $this->core->auction( $l, $data ) );
    }
    
    /**
    * close an auction and cache it.
    */
    public function close( $id, array $data = NULL ){
        return $this->writeToCache( $this->core->close( $id, $data ) );
    }
    
   /**
    * buy now, and cache results.
    */
    public function buy($id, array $data = NULL ){
        return $this->writeToCache( $this->core->buy( $id, $data ) );
    }
    
   /**
    * bid, and cache results.
    */
    public function bid( $id, $bid, array $data = NULL ){
        return $this->writeToCache( $this->core->bid( $id, $bid, $data ) );
    }
    
   /**
    * grab a list of auctions by id from the cache and repopulate missing records from the db.
    */
    public function fetch( array $ids, $lock = FALSE){
        if( $lock ) return $this->core()->fetch( $ids, $lock );
        $cache = $this->cache();
        $timeout = $this->cacheTimeout();
        if( count( $ids ) < 1 ) return array();
        if( $this->refresh ){
            $rows = $this->core->fetch( $ids, FALSE );
            foreach( $rows as $k => $v ) $cache->set($k, $v, $timeout ); 
            return $rows;
        }
        $options = array(
            'callback'=> array( $this->core, 'fetch'), // callback handler for missing rows.
            'method'=>'set', // use add method, so we don't accidentally clobber updates to the cache.
            'timeout' => $timeout, // how long do we want the data cached?
            'cache_missing'=> TRUE, // missing keys are stored in the cache, so we don't keep hitting the db.
        );
        $cache = new Store\Callback($cache, $options );
        $result = $cache->get( $ids );
        if( ! is_array( $result ) ) $result = array();
        return $result;
    }
    
   /**
    * cache searches by creating a checksum of the search parameters
    * to keep the cache more accurate we also keep a dynamic cache-bust key for certain parameters<commit_msg>reverting to prior behavior<commit_after><?php
namespace Gaia\Souk;
use Gaia\Store;
use Gaia\DB\Transaction;

/**
* This class is a wrapper class around Souk that allows us to cache the results in a write-thru
* approach. 
*/
class Cacher extends Passthru {
   
   /**
    * how long to cache the data
    */
    const CACHE_TIMEOUT = 86400;
    
    /**
    * cache searches differently because the cache becomes dirty quickly. 
    */
    const SEARCH_CACHE_TIMEOUT = 600;
    
    /**
    * cache prefix key
    */
    const SEARCH_VECTOR_PREFIX =  'sv01/';
    
    /**
    * refresh the cache, when reading?
    * @see self::forceRefresh
    */
    protected $refresh = FALSE;
    
    protected $cacher;
    
    public function __construct( Iface $core, Store\Iface $cacher ){
        parent::__construct( $core );
        $this->cacher = $cacher;
    }
    
    /**
    * perform the auction and store the results in the cache.
    */
    public function auction( $l, array $data = NULL ){
        return $this->writeToCache( $this->core->auction( $l, $data ) );
    }
    
    /**
    * close an auction and cache it.
    */
    public function close( $id, array $data = NULL ){
        return $this->writeToCache( $this->core->close( $id, $data ) );
    }
    
   /**
    * buy now, and cache results.
    */
    public function buy($id, array $data = NULL ){
        return $this->writeToCache( $this->core->buy( $id, $data ) );
    }
    
   /**
    * bid, and cache results.
    */
    public function bid( $id, $bid, array $data = NULL ){
        return $this->writeToCache( $this->core->bid( $id, $bid, $data ) );
    }
    
   /**
    * grab a list of auctions by id from the cache and repopulate missing records from the db.
    */
    public function fetch( array $ids, $lock = FALSE){
        if( $lock ) return $this->core()->fetch( $ids, $lock );
        $cache = $this->cache();
        $timeout = $this->cacheTimeout();
        if( count( $ids ) < 1 ) return array();
        if( $this->refresh ){
            $rows = $this->core->fetch( $ids, FALSE );
            foreach( $rows as $k => $v ) $cache->set($k, $v, $timeout ); 
            return $rows;
        }
        $options = array(
            'callback'=> array( $this->core, 'fetch'), // callback handler for missing rows.
            'method'=>'add', // use add method, so we don't accidentally clobber updates to the cache.
            'timeout' => $timeout, // how long do we want the data cached?
            'cache_missing'=> TRUE, // missing keys are stored in the cache, so we don't keep hitting the db.
        );
        $cache = new Store\Callback($cache, $options );
        $result = $cache->get( $ids );
        if( ! is_array( $result ) ) $result = array();
        return $result;
    }
    
   /**
    * cache searches by creating a checksum of the search parameters
    * to keep the cache more accurate we also keep a dynamic cache-bust key for certain parameters
{'fraction_non_alphanumeric': 0.13565629228687415, 'fraction_numerical': 0.0037212449255751017, 'mean_word_length': 2.125264270613108, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6310401', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1764, 'n_words': 602}