<commit_before><commit_msg>Utility for fetching, formatting and caching recent additions to the image archive<commit_after><?php

/**
 * Recent additions
 * @since Version 3.10.0
 * @package Railpage
 * @author <NAME>
 */

namespace Railpage\Images\Utility; 

use Railpage\Images\Images;
use Railpage\Images\ImageCache;
use Railpage\AppCore;
use Railpage\Debug;
use Memcached as PhpMemcached;

class Recent {
    
    /**
     * Get $num newest images
     * @since Version 3.10.0
     * @param int $num
     * @return array
     * @todo $cacheProvider doesn't seem to bloody work!
     */
    
    public static function getNewest($num = 5) {
        
        $host = defined("RP_MEMCACHE_HOST") ? RP_MEMCACHE_HOST : "cache.railpage.com.au";
        $port = defined("RP_MEMCACHE_PORT") ? RP_MEMCACHE_PORT : 11211;
        
        $cacheProvider = new PhpMemcached; 
        $cacheProvider->addServer($host, $port); 
        
        $mckey = sprintf("railpage:images.recent=%d;url.cached", $num); 
        
        if ($newphotos = $cacheProvider->get($mckey)) {
            Debug::LogCLI("Fetched new photos from cache provider using cache key " . $mckey); 
            return $newphotos; 
        }
        
		$newphotos = (new Images)->getRecentAdditions(5); 
		shuffle($newphotos); 
        
        foreach ($newphotos as $id => $data) {
            $newphotos[$id]['meta']['sizes']['medium']['source'] = ImageCache::cache($newphotos[$id]['meta']['sizes']['medium']['source']);
        }
        
        $rs = $cacheProvider->set($mckey, $newphotos, strtotime("+15 minutes")); 
        Debug::LogCLI("Saved new photos in cache provider using cache key " . $mckey); 
        
        if ($res = $cacheProvider->get($mckey)) {
            Debug::LogCLI("new photos found in cache, success"); 
        }
        
        return $newphotos; 
        
    }
}
{'fraction_non_alphanumeric': 0.13802363050483352, 'fraction_numerical': 0.009129967776584319, 'mean_word_length': 2.541825095057034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '6586706', 'n_tokens_mistral': 598, 'n_tokens_neox': 553, 'n_words': 129}