Beispiel #1
0
	typename std::enable_if<(sizeof...(Args), std::is_same<cache_t, cache_vector_t>::value) && tp == timeout_policy_t::NEVER, std::remove_const_t<std::decay_t<R>>>::type
	const& call(Args const&... args)
	{
		decayed_key_t new_keys(getKey(args)...);
		auto&& it(cache.find(new_keys));

		if (it == cache.end())      // not cached
		{
			if (cache.size() == LRU_MAX)        // cache is full, time to overwrite old objects
			{
				if (internal_it == internal_cache.end())
				{
					assert(cache_it == cache.end());
					internal_it = internal_cache.begin();
					cache_it = cache.begin();
				}

				*internal_it = fptr(args...);
				cache.erase(cache_it);
				cache.insert(cache_it, std::pair<key_t, internal_cache_it_t>(new_keys, internal_it));
				++cache_it;
				return *internal_it++;
			}
			else
			{
				// Maybe emplace_back
				internal_cache.push_back(fptr(args...));
				cache[new_keys] = internal_cache.end();
				return internal_cache.back();
			}
		}
		else                    // was cached
			return *it->second;
	};
	void get( const LLUUID& key, callback_slot_t slot )
	{
		if(key.isNull()) return;

		cache_t::const_iterator it = sCache.find(key);
		if (it != sCache.end())
		{
			// ...name already exists in cache, fire callback now
			callback_signal_t signal;
			signal.connect(slot);
			
			signal(it->second);
			return;
		}

		fetch(key);

		// always store additional callback, even if request is pending
		signal_map_t::iterator sig_it = sSignalMap.find(key);
		if (sig_it == sSignalMap.end())
		{
			// ...new callback for this id
			callback_signal_t* signal = new callback_signal_t();
			signal->connect(slot);
			sSignalMap[key] = signal;
		}
		else
		{
			// ...existing callback, bind additional slot
			callback_signal_t* signal = sig_it->second;
			signal->connect(slot);
		}
	}
	void eraseExpired()
	{
		F64 now = LLFrameTimer::getTotalSeconds();
		cache_t::iterator it = sCache.begin();
		while (it != sCache.end())
		{
			cache_t::iterator cur = it;
			LLSD& exp = cur->second;
			++it;

			if(exp.has(EXPIRES) && exp[EXPIRES].asReal() < now)
			{
                if(!exp.has(EXPERIENCE_ID))
				{
                    LL_WARNS("ExperienceCache") << "Removing experience with no id " << LL_ENDL ;
                    sCache.erase(cur);
					}
                else
                {
                    LLUUID id = exp[EXPERIENCE_ID].asUUID();
                    LLUUID private_key = exp.has(LLExperienceCache::PRIVATE_KEY) ? exp[LLExperienceCache::PRIVATE_KEY].asUUID():LLUUID::null;
                    if(private_key.notNull() || !exp.has("DoesNotExist"))
					{
						fetch(id, true);
					}
					else
					{
                        LL_WARNS("ExperienceCache") << "Removing invalid experience " << id << LL_ENDL ;
						sCache.erase(cur);
					}
				}
			}
		}
	}
	void erase( const LLUUID& key )
	{
		cache_t::iterator it = sCache.find(key);
				
		if(it != sCache.end())
		{
			sCache.erase(it);
		}
	}
	bool fetch( const LLUUID& key, bool refresh/* = true*/ ) 
	{
		if(!key.isNull() && !isRequestPending(key) && (refresh || sCache.find(key)==sCache.end()))
		{
			LL_DEBUGS("ExperienceCache") << " queue request for " << EXPERIENCE_ID << " " << key << LL_ENDL ;
			sAskQueue[key]=EXPERIENCE_ID;

			return true;
		}
		return false;
	}
	const LLSD& get(const LLUUID& key)
	{
		if(key.isNull()) return empty;
		cache_t::const_iterator it = sCache.find(key);

		if (it != sCache.end())
		{
			return it->second;
		}

		fetch(key);

		return empty;
	}
Beispiel #7
0
	typename std::enable_if<(sizeof...(Args), std::is_same<cache_t, cache_map_t>::value) && tp == timeout_policy_t::NEVER, std::remove_const_t<std::decay_t<R>>>::type
	const& call(Args const&... args)
	{
		decayed_key_t new_keys(getKey(args)...);
		auto&& it(cache.find(new_keys));

		if (it == cache.end())      //not cached
		{
			auto&& pos(cache.insert(std::pair<key_t, R>(std::move(new_keys), fptr(args...))));

			return pos.first->second;
		}
		else                        //was cached
			return it->second;
	};
Beispiel #8
0
int cycleLengthRecursive(unsigned int n) {
  // base case is handled by cache
  
  cache_t::iterator it = cache.find(n);
  if (it != cache.end()) {
    return it->second;  
  }
  
  int out;
  if (n % 2 == 1) 
    out = 1 + cycleLengthRecursive((n*3) + 1);  // if n is odd then  n <- 3n + 1
  else           
    out = 1 + cycleLengthRecursive(n/2);        // else n <- n/2

  cache[n] = out;
  return out;
}
	void exportFile(std::ostream& ostr)
	{
		LLSD experiences;

		cache_t::const_iterator it =sCache.begin();
		for( ; it != sCache.end() ; ++it)
		{
			if(!it->second.has(EXPERIENCE_ID) || it->second[EXPERIENCE_ID].asUUID().isNull() ||
				it->second.has("DoesNotExist") || (it->second.has(PROPERTIES) && it->second[PROPERTIES].asInteger() & PROPERTY_INVALID))
				continue;

			experiences[it->first.asString()] = it->second;
		}

		LLSD data;
		data["experiences"] = experiences;

		LLSDSerialize::toPrettyXML(data, ostr);
	}
Beispiel #10
0
// static
bool Dbase::CacheGet(Cddb::Matches& res, const Cddb::discid_t discID)
{
    bool ret = false;
    for (cache_t::const_iterator it = s_cache.find(discID); it != s_cache.end(); ++it)
    {
        if (it->discID == discID)
        {
            ret = true;
            res.discID = discID;
            LOG(VB_MEDIA, LOG_DEBUG, QString("Cddb CacheGet %1 found %2 ")
                .arg(discID,0,16).arg(it->discID,0,16)
                + it->discGenre + " " + it->artist + " / " + it->title);

            // If it's marker for 'no match' then genre is empty
            if (!it->discGenre.isEmpty())
                res.matches.push_back(Cddb::Match(*it));
        }
    }
    return ret;
}
Beispiel #11
0
 //start = startIndex
 int mx(int start, int maxw)
 {
    if(start >= tt->size() || maxw <= 0) return 0;
    cache_t::iterator it = cache.find(PI(start, maxw));
    if(it != cache.end()){
       return it->second;
    }
    int without = mx(start+1, maxw);
    int res = without;
    int st_w = (*tt)[start].w;
    int st_st = (*tt)[start].st;
    int with = -1;
    if(st_w <= maxw){
       int new_maxw = min(maxw - st_w, st_st - st_w);
       with = mx(start+1, new_maxw);
       if(1+with > res){
          res = 1+with;
       }
    }
    cache[PI(start,maxw)] = res;
    return res;
 }