示例#1
0
	void on_schedule(Struct_type *page)
	{
		if (is_full())
		{
			evict_one();
		}

		new_schedule(page);
	}
示例#2
0
LOCAL_CACHE_TEMPLATE
typename LOCAL_CACHE_CLASS::result
LOCAL_CACHE_CLASS::put_if_absent(const KeyT &key, const ValueT &val, ValueT &curtVal) {
    if (capacity < 1) {
        return MISS;
    }
    result res = MISS;
    // lock
    tbsys::CThreadGuard guard(&mutex);

    entry_cache_iterator iter = cache.find(&key);
    internal_entry *entry = NULL;
    if (iter == cache.end()) {
        res = MISS;
        // not found
        // evict entry
        while (cache.size() >= capacity) {
            assert(capacity >= 1);
            const internal_entry *evict = evict_one();
            assert(evict != NULL);
            // free entry
            drop_entry(evict);
        }

        // insert new one
        // allocate internal_entry, relase after evict or in clear()
        entry = make_entry(key, val);
        if (entry == NULL) {
            return SETERROR;
        }
        lru.push_front(entry);
        cache[entry->get_key()] = lru.begin();
        curtVal = val;
    } else {
        res = HIT;
        // found, already exists
        // adjust lru list
        lru.splice(lru.begin(), lru, iter->second);
        // update entry value and utime
        // update first, some meta info
        fill_value(iter, curtVal);
        uint64_t now = tbutil::Time::now().toMilliSeconds();
        assert(expire != 0);
        if (now - entry_utime(iter) > expire) {
            // expired
            set_entry_utime(iter->second, now);
            res = EXPIRED;
        }
    }
    return res;
}
示例#3
0
LOCAL_CACHE_TEMPLATE
void LOCAL_CACHE_CLASS::clear() {
    // lock
    tbsys::CThreadGuard guard(&mutex);
    // evict every entry
    while (cache.size() > 0) {
        const internal_entry *evict = evict_one();
        assert(evict != NULL);
        // free entry
        drop_entry(evict);
    }
    lru.clear();
    cache.clear();
}
示例#4
0
LOCAL_CACHE_TEMPLATE
void LOCAL_CACHE_CLASS::put(const KeyT& key, const ValueT& val) 
{
  if (capacity < 1) {
      return ;
  }
  // lock 
  tbsys::CThreadGuard guard(&mutex);

  entry_cache_iterator iter = cache.find(&key);
  internal_entry *entry = NULL;
  if (iter == cache.end()) {
    // not found
    // evict entry
    while (cache.size() >= capacity) {
      assert(capacity >= 1);
      const internal_entry *evict = evict_one();
      assert(evict != NULL);
      // free entry
      drop_entry(evict);
    }

    // insert new one 
    // allocate internal_entry, relase after evict or in clear()
    entry = make_entry(key, val);
    if (entry == NULL) {
      return ;
    }
    lru.push_front(entry);   
  } else {
    // found, already exists
    // adjust lru list
    lru.splice(lru.begin(), lru, iter->second); 
    // update entry value and utime
    // update first, some meta info
    cache.erase(iter);
    entry_list_iterator elem = lru.begin();
    set_entry_key(elem, key);
    set_entry_value(elem, val);
    set_entry_utime_now(elem);
    entry = *elem;
  }
  cache[&(entry->key)] = lru.begin();
}