I'm working on Janky in C#, and I have a JankyToken
class. I'm gonna be making a lot of JankyToken
s, so I need to know, how can I make a method (Create
) of JankyToken
that will cache it?
This is what I have so far:
// NOTE: `Type` is short for `JankyToken.Type` which is an
// enum that describes the token (e.g. `GT`, `LTEQ`, `ASS`, etc...).
/**
* @param type The type of token.
* @param value Additional data that describes custom tokens (e.g. Identifiers).
*/
public JankyToken Create(Type type, String value) {
cache = /* Some method of retrieving the cached params */;
if(cache != null) return cache;
else {
result = new JankyToken(type, value);
/* Some method of caching the result */
return cache;
}
}
Thanks!
Cheers!
Cheers!
Top comments (4)
This is a simple implementation of cache.
If you use threads, then better use Concurrent Dictionary.
docs.microsoft.com/dotnet/api/syst...
For example:
What if I want to retain the original value(s) of the parameters, like Python's LRU cache?
I'm not good at Python, but if I understood you correctly, you have need retain Type type and string val into cache. In this case you can define a new class and implements properties for these params or use Tuple types.
ASP.NET Core or .NET 5 have ready implementation of cache
For example
Or for C# 9
Okay. Thanks for your help.