Question Caching a custom query using vboptimise?

Status
Not open for further replies.

tavenger5

Customer
I'm trying to figure out how to pass a custom query to vboptimize to cache it. I see that vb_optimise::cache is the function. Can pass it by literally saying vb_optimise::cache($queryvariable) ? What files would have to be included?
 
Last edited:
Here's a couple functions from the APTL mod that may help:
PHP:
    /**    * Reads from the vB Optimise cache
    *
    * @param    string    Referencing the vB Optimise option varname
    * @param    string    The cache key to read from
    */
    public static function read($cacheType, $key)
    {
        if (!self::_canCache($cacheType))
        {
            // We can't cache this
            return -1;
        }


        // Fetch the vBO data
        $_data = vb_optimise::$cache->get('dbtech.thanks.' . $key);


        if (is_array($_data) AND TIMENOW < $_data['time'])
        {
            // We saved some queries
            vb_optimise::stat(1);
            vb_optimise::report('Fetched dbtech.thanks.' . $key . ' from cache successfully.');


            return $_data['cache'];
        }


        return false;
    }


    /**
    * Writes to the vB Optimise cache
    *
    * @param    string    Database table we are working with
    * @param    string    (Optional) Any additional clauses to the query
    */
    public static function write($data, $cacheType, $key)
    {
        if (!self::_canCache($cacheType))
        {
            // We can't cache this
            return false;
        }


        // By default, we want to "null out" the cache
        $_data = false;


        if ($data !== false)
        {
            // Write the vBO data
            $_data = array(
                'time'    => TIMENOW + (self::$vbulletin->options['vbo_cache_thanks' . $cacheType] * 3600),
                'cache'    => $data,
            );
        }


        // Write the cache
        vb_optimise::$cache->set('dbtech.thanks.' . $key, $_data);
        vb_optimise::report('Cached dbtech.thanks.' . $key . ' successfully.');    


        return true;
    }


    /**
    * Writes to the vB Optimise cache
    */
    public static function flush()
    {
        if (!self::_canCache())
        {
            // We can't cache this
            return false;
        }


        // Flush the cache
        vb_optimise::$cache->flush();


        return true;
    }


    /**
    * Tests whether we can cache something
    *
    * @param    string    Original message
    * @param    string    Overriding
    */    
    protected static function _canCache($cacheType = '')
    {
        if (!class_exists('vb_optimise'))
        {
            // We don't have vBO installed
            return false;
        }


        if (!isset(self::$vbulletin->options['vbo_online']))
        {
            // vBO is turned off
            return false;
        }


        if (!is_object(vb_optimise::$cache))
        {
            // Not a valid cache object
            return false;
        }


        if (!$cacheType)
        {
            // This will be used for the flush
            return true;
        }


        if (!isset(self::$vbulletin->options['vbo_cache_thanks' . $cacheType]))
        {
            // vBO's version is too old
            return false;
        }


        if (!self::$vbulletin->options['vbo_cache_thanks' . $cacheType])
        {
            // The cache time has been turned off
            return false;
        }


        return true;
    }
 
flush() would remove ALL data from the cache, if you notice in the write() function it has support for automatic cache invalidation. You'd obviously replace the vBOption references with hardcoded values :)
 
Status
Not open for further replies.

Legacy vB Optimise

vBulletin 3.8.x vBulletin 4.x.x
Seller
DragonByte Technologies
Release date
Last update
Total downloads
1,972
Customer rating
0.00 star(s) 0 ratings
Back
Top