Reputation: 57
I am trying to incorporate some logic into an application which will improve its performance but I simply can't wrap around a good way to implement it. So I have a set of json files which I deserialize from and all these files have the same structure.
{
urlSouce": """,
"info": [
{
"id": ""
}
]
}
So each time I Deserialize a file I urlSOurce in a variable and use that value for an API request. However I have understood that most of the time this urlSource is the same and hence keep sending an API request is not needed and affecting performance which is why I first want to check if the urlSource coming in from a currently deserializing file is the same as the previous file.
If so, there is no need to send the API request again . I understand I can us an boolean flag to add in this logic but I can't think of where is the best place to keep it.
To demonstrate what I am doing I have the following code. If anyone has any idea on what needs to be done please help me
string previousUrl;
string currentURL;
bool isValueChanged;
currentURL = "test1.com"; //the new URL populated from the deserialized JSON
previousUrl = "test2.com"; //the previously deserialized URL of the file before this
Here I have no problem with any of the deserialization
The problem I have is how do I keep track of the previousURl value ? and how can I manipulate the booleanFlag?
Upvotes: 0
Views: 54
Reputation: 37070
It sounds like you want to validate that a string has not been used previously before you process it.
If that's the case, you could simply store the "used" strings in a Dictionary
, with the string as the Key
and the result of processing as the Value
, and then just check if the dictionary already contains a Key
for any new strings before processing them.
The method below takes in a string
to process and a bool
specifying whether or not to use a cached result if it exists. If the string
is not contained in the dictionary, or the user specifies useCache = false
, the string is processed and saved in the dictionary (along with the result), and the result is returned. Otherwise the cached result for that string is returned right away.
private readonly Dictionary<string, string> cachedResults = new Dictionary<string, string>();
public string GetApiResult(string uri, bool useCachedResult = true)
{
// If we've already encountered this string, return the cached result right away
if (useCachedResult && cachedResults.ContainsKey(uri)) return cachedResults[uri];
// Process the string here
var result = MakeAPICall(uri);
// Save it, along with the result, in our dictionary
cachedResults[uri] = result;
// Return the result
return result;
}
It sounds like I misunderstood the question. If you're only trying to determine if a new string matches the last string encountered, then that's also very simple:
private string cachedUri;
private string cachedResult;
public string GetApiResult(string uri, bool useCachedResult = true)
{
// If we've already encountered this string, return the cached result right away
if (useCachedResult && uri == cachedUri) return cachedResult;
// Process the string here
var result = MakeAPICall(uri);
// Save it, along with the result, in our fields
cachedUri = uri;
cachedResult = result;
// Return the result
return result;
}
Upvotes: 1
Reputation: 100630
I'd use MemoryCache
(or just Dictionary
) with Url as a key and response's result as value, but if there usually just one url variables as you have could be enough:
string previousUrl = null;
string previousValue = null;
string currentURL;
bool isValueChanged;
while (…)
{
string result = null;
currentURL = "test1.com"; //the new URL populated from the deserialized JSON
if (previousUrl != currentUrl)
{
previousResult = GetTheData(currentUrl);
previousUrl = currentUrl;
}
result = previousResult;
// process result for currentUrl
}
Upvotes: 0