You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
788 B
41 lines
788 B
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace FLocal.Core {
|
|
public class Cache<T> {
|
|
|
|
public static readonly Cache<T> instance = new Cache<T>();
|
|
|
|
private Dictionary<object, T> cache;
|
|
|
|
private Cache() {
|
|
this.cache = new Dictionary<object,T>();
|
|
}
|
|
|
|
public T get(object id, Func<T> getter) {
|
|
if(!this.cache.ContainsKey(id)) {
|
|
lock(id) {
|
|
if(!this.cache.ContainsKey(id)) {
|
|
this.cache[id] = getter();
|
|
}
|
|
}
|
|
}
|
|
try {
|
|
return this.cache[id];
|
|
} catch(KeyNotFoundException) {
|
|
return this.get(id, getter);
|
|
}
|
|
}
|
|
|
|
public void delete(object id) {
|
|
lock(id) {
|
|
if(this.cache.ContainsKey(id)) {
|
|
this.cache.Remove(id);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|