<commit_before><?php

namespace diversen;

use diversen\db\q;



/**
 * Class cache. 3 methods set, get, and delete
 * 
 * When setting something with the cache class you will specify a string and
 * a ID. Name is given in order to prevent class of identical ID's only.
 * This can be e.g. the module name if your module uses the cache.  
 * 
 * When the class gets a name and ID it generates a unique string 
 * which is the internal cache key. 
 * 
 * When retrieving information you will only need to specify the 
 * a original (e.g. the module name) and the ID. 
 * 
 * <code>
 * 
 * $data = array ('title' => 'test', 'content' => 'some content');
 * 
 * 
 * cache::set('my_blog' 123, $data);
 * 
 * // getting the entry is just as easy. 
 * // The last argument is max life time in seconds for the entry. 
 * cache::get('my_blog', 123, 3600);
 * 
 * // if the entry is outdated NULL will be returned
 * 
 * </code>
 * 
 * 
 * @package cache
 * 
 */
class cache {

        
    /**
     * DB cache table
     * @var string
     */
    public static $table = 'system_cache';

    /**
     * Generate a system cache id
     * @param   string    $module
     * @param   int       $id
     * @return  string    $str (md5)
     */
    public static function generateId($module, $id) {
        $str = $module . '_' . $id;
        return md5($str);
    }

    /**
     * get a cached string from a module and an id
     * the module and the id can be anything, but for the sake of not 
     * cluttering the cache namespace, this is a standard that can be used
     * @param   string    $module
     * @param   int       $id
     * @param   int       $max_life_time in secs
     * @return  mixed     $data unserialized data
     */
    public static function get($module, $id, $max_life_time = null) {
        return self::getDb($module, $id, $max_life_time);
    }

    /**
     * Sets a string in cache
     * @param   string  $module
     * @param   int     $id
     * @param   string  $data
     * @return  string   $str
     */
    public static function set($module, $id, $data) {
        return self::setDb($module, $id, $data);
    }

    /**
     * delete a string from cache
     * @param   string  $module
     * @param   int     $id
     * @return  boolean $res db result
     */
    public static function delete($module, $id) {
        return self::deleteDb($module, $id) ;
    }

    /**
     * get a cached string from a module and an id
     * the module and the id can be anything, but for the sake of not 
     * cluttering the cache namespace, this is a standard that can be used
     * @param   string    $module
     * @param   int       $id
     * @param   int       $max_life_time in secs
     * @return  mixed     $data unserialized data
     */
    private static function getDb($module, $id, $max_life_time = null) {
        $md5 = self::generateId($module, $id);
        $row = q::select(self::$table)->filter('id =', $md5)->fetchSingle();

        if (!$row) {
            return null;
        }
        if ($max_life_time) {
            $expire = $row['unix_ts'] + $max_life_time;
            if ($expire < time()) {
                self::delete($module, $md5);
                return null;
            } else {
                return unserialize($row['data']);
            }
        } else {
            return unserialize($row['data']);<commit_msg>only null indicates that no cache value match<commit_after><?php

namespace diversen;

use diversen\db\q;



/**
 * Class cache. 3 methods set, get, and delete
 * 
 * When setting something with the cache class you will specify a string and
 * a ID. Name is given in order to prevent class of identical ID's only.
 * This can be e.g. the module name if your module uses the cache.  
 * 
 * When the class gets a name and ID it generates a unique string 
 * which is the internal cache key. 
 * 
 * When retrieving information you will only need to specify the 
 * a original (e.g. the module name) and the ID. 
 * 
 * <code>
 * 
 * $data = array ('title' => 'test', 'content' => 'some content');
 * 
 * 
 * cache::set('my_blog' 123, $data);
 * 
 * // getting the entry is just as easy. 
 * // The last argument is max life time in seconds for the entry. 
 * cache::get('my_blog', 123, 3600);
 * 
 * // if the entry is outdated NULL will be returned
 * 
 * </code>
 * 
 * 
 * @package cache
 * 
 */
class cache {

        
    /**
     * DB cache table
     * @var string
     */
    public static $table = 'system_cache';

    /**
     * Generate a system cache id
     * @param   string    $module
     * @param   int       $id
     * @return  string    $str (md5)
     */
    public static function generateId($module, $id) {
        $str = $module . '_' . $id;
        return md5($str);
    }

    /**
     * get a cached string from a module and an id
     * the module and the id can be anything, but for the sake of not 
     * cluttering the cache namespace, this is a standard that can be used
     * @param   string    $module
     * @param   int       $id
     * @param   int       $max_life_time in secs
     * @return  mixed     $data unserialized data
     */
    public static function get($module, $id, $max_life_time = null) {
        return self::getDb($module, $id, $max_life_time);
    }

    /**
     * Sets a string in cache
     * @param   string  $module
     * @param   int     $id
     * @param   string  $data
     * @return  string   $str
     */
    public static function set($module, $id, $data) {
        return self::setDb($module, $id, $data);
    }

    /**
     * delete a string from cache
     * @param   string  $module
     * @param   int     $id
     * @return  boolean $res db result
     */
    public static function delete($module, $id) {
        return self::deleteDb($module, $id) ;
    }

    /**
     * get a cached string from a module and an id
     * the module and the id can be anything, but for the sake of not 
     * cluttering the cache namespace, this is a standard that can be used
     * @param   string    $module
     * @param   int       $id
     * @param   int       $max_life_time in secs
     * @return  mixed     $data unserialized data
     */
    private static function getDb($module, $id, $max_life_time = null) {
        $md5 = self::generateId($module, $id);
        $row = q::select(self::$table)->filter('id =', $md5)->fetchSingle();

        if (!$row) {
            return null;
        }
        if ($max_life_time OR $max_life_time === 0) {
            $expire = $row['unix_ts'] + $max_life_time;
            if ($expire < time()) {
                self::delete($module, $md5);
                return null;
            } else {
                return unserialize($row['data']);
            }
        } else {
            return unserialize($row['data']);
{'fraction_non_alphanumeric': 0.11541850220264317, 'fraction_numerical': 0.004845814977973568, 'mean_word_length': 1.974235807860262, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '3894781', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2012, 'n_words': 755}