C# でオブジェクトの "順序付けされた" キャッシュを作成しようとしています。この順序は、アクセスされた回数によって決定されます。
Dictionary、SortedList、および SortedDictionary を調べましたが、これらは非常に近いものでしたが、探しているものがまったくありません。
以前にキャッシュされたすべてのアイテムを含むリストが必要です。これらのアイテムにはgetHits()
、キャッシュされたアイテムの順序を決定するメソッドを含めることができます。
次に、そのキャッシュに名前でアクセスして、アイテムが表示された回数を増やすことができます。
簡略化された例 (疑似 C#で):
class Result {
public int Hits = 0;
public string Name = "";
public void IncreaseHits() {
this.hits++;
}
public Result(String name) {
this.name = name;
}
}
class Program {
public MagicSortableType<string, Result> MyCache; //what structure to use?
public main() {
MyCache.Add(new Result("My result 1"));
MyCache.Add(new Result("My result 2"));
MyCache.Add(new Result("My result 3"));
MyCache['My result 2'].IncreaseHits();
MyCache['My result 2'].IncreaseHits();
MyCache['My result 3'].IncreaseHits();
MyCache.SortDesc(); //what is the real C# equivalent?
foreach(Result result in MyCache) {
Console.Write(result.Name + " - hits " + result.Hits);
}
}
}
出力:
My result 2 - hits 2
My result 3 - hits 1
My result 1 - hits 0