Пример #1
0
        private CacheEntry PutData(string key, byte[] data, long ms_cost)
        {
            var now = DateTime.UtcNow;
            lock(cache){
                cache.changes_since_cleanse++;
                CacheEntry entry;
                if (cache.hash.TryGetValue(key, out entry)){
                    if (entry.data == null) entry.recreated_count++;
                }else {
                    entry = new CacheEntry();
                    cache.hash[key] = entry;
                    entry.written = now;
                    entry.loaded = now;
                }
                entry.data = data;
                entry.sizeInBytes = data.Length;
                entry.cost_ms = (int)ms_cost;

                MarkRead(entry);
                return entry;
            }
        }
Пример #2
0
        private bool TryGetData(string key, out byte[] data, out CacheEntry entry)
        {
            var now = DateTime.UtcNow;
            lock(cache){
                if (cache.hash.TryGetValue(key, out entry)){
                    //Take a reference before we exit the lock
                    data = entry.data;

                    if (data != null){
                        MarkRead(entry);
                        return true;
                    };
                }
            }
            data = null;
            return false;
        }
Пример #3
0
 private void MarkRead(CacheEntry e)
 {
     var now = DateTime.UtcNow;
     if (e.recent_reads == null) e.recent_reads = new Queue<DateTime>(8);
     if (e.recent_reads.Count > 7) e.recent_reads.Dequeue();
             e.recent_reads.Enqueue(now);
             e.read_count++;
 }