Click here to Skip to main content
15,890,438 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: A Programming Philosophy Question Pin
Gary Wheeler3-Sep-19 1:17
Gary Wheeler3-Sep-19 1:17 
GeneralRe: A Programming Philosophy Question Pin
honey the codewitch31-Aug-19 17:45
mvahoney the codewitch31-Aug-19 17:45 
GeneralRe: A Programming Philosophy Question Pin
User 26235231-Aug-19 23:02
User 26235231-Aug-19 23:02 
GeneralRe: A Programming Philosophy Question Pin
Chris Losinger1-Sep-19 3:34
professionalChris Losinger1-Sep-19 3:34 
GeneralRe: A Programming Philosophy Question Pin
KateAshman1-Sep-19 21:39
KateAshman1-Sep-19 21:39 
GeneralRe: A Programming Philosophy Question Pin
Richard Andrew x642-Sep-19 4:07
professionalRichard Andrew x642-Sep-19 4:07 
GeneralRe: A Programming Philosophy Question Pin
User 5838522-Sep-19 19:18
User 5838522-Sep-19 19:18 
GeneralHappy coding. Pin
honey the codewitch31-Aug-19 12:28
mvahoney the codewitch31-Aug-19 12:28 
When an object model you didn't design with caching in mind can implement intelligent caching with very little code changes.

I backed all my objects with normalized json - basically an object is represented by a Dictionary and an array is represented by a List

each property already did localized caching for individual fiels

C#
public string Title 
{ 
  get {
    return GetCachedProperty("title",(string)null);
  }
}

and has a public Json property that contains the root for that object.

All I do is create a larger Json object and root each object in that. When an instance is created, the first thing it does is root itself in the larger cache document. Like so

C#
void _InitializeCache()
{
	// Json is a new object with our key(s) in it.
	var networks = Tmdb.GetProperty("networks", (IDictionary<string, object>)null);
	if (null == networks)
	{
		networks = new JsonObject();
		Tmdb.Cache.Add("networks", networks);
	}
	object o;
	if (networks.TryGetValue(Id.ToString(), out o))
	{
		var oj = Json;
		var d = o as IDictionary<string, object>;
		if (null != d)
		{
			// found our network:
			// 1. take any existing data and merge it with the cache
			// 2. set our Json pointer to the cache for it
			JsonObject.MergeReplace(oj, d); // merges one tree with another
			Json = d;
		}
	}
	else
		networks.Add(Id.ToString(), Json);
}



Since the Json graph is all objects, it keeps references intact so you can reference the same branch from multiple places and there will only be one copy, even though if you serialize the JSON out each reference will be written out (so N copies)


Anyway, what's cool is you can check the cache simply by calling

Console.WriteLine(Tmdb.Cache); // basically an IDictionary object with an overloaded ToString method.

or clear it by calling Tmdb.Cache.Clear();

or traverse it as lists and dictionaries.

And it can easily be serialized and deserialized (as long as your cross references don't hose it too badly)

It's pretty cool overall.

I just designed it to back The Movie Database's JSON/REST api but in doing so I made the caching completely automatic without even changing much of my code.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

GeneralSeems about right Pin
Jörgen Andersson31-Aug-19 9:54
professionalJörgen Andersson31-Aug-19 9:54 
GeneralRe: Seems about right Pin
OriginalGriff31-Aug-19 19:49
mveOriginalGriff31-Aug-19 19:49 
GeneralRe: Seems about right Pin
Jörgen Andersson31-Aug-19 21:42
professionalJörgen Andersson31-Aug-19 21:42 
GeneralRe: Seems about right Pin
OriginalGriff31-Aug-19 21:57
mveOriginalGriff31-Aug-19 21:57 
GeneralRe: Seems about right Pin
Jörgen Andersson31-Aug-19 22:13
professionalJörgen Andersson31-Aug-19 22:13 
GeneralRe: Seems about right Pin
OriginalGriff31-Aug-19 22:42
mveOriginalGriff31-Aug-19 22:42 
GeneralRe: Seems about right Pin
Sander Rossel31-Aug-19 23:01
professionalSander Rossel31-Aug-19 23:01 
GeneralRe: Seems about right Pin
Gary Wheeler3-Sep-19 1:20
Gary Wheeler3-Sep-19 1:20 
GeneralPhoto's - Remembering the battle of the Scheldt Pin
RickZeeland31-Aug-19 2:11
mveRickZeeland31-Aug-19 2:11 
GeneralRe: Photo's - Remembering the battle of the Scheldt Pin
Richard MacCutchan31-Aug-19 4:57
mveRichard MacCutchan31-Aug-19 4:57 
GeneralAdobe got it right... Pin
Sander Rossel31-Aug-19 0:50
professionalSander Rossel31-Aug-19 0:50 
GeneralRe: Adobe got it right... Pin
Ron Anders31-Aug-19 2:31
Ron Anders31-Aug-19 2:31 
GeneralRe: Adobe got it right... Pin
Mike Hankey31-Aug-19 4:50
mveMike Hankey31-Aug-19 4:50 
GeneralRe: Adobe got it right... Pin
dandy7231-Aug-19 7:26
dandy7231-Aug-19 7:26 
GeneralRe: Adobe got it right... Pin
Mike Hankey31-Aug-19 14:59
mveMike Hankey31-Aug-19 14:59 
GeneralRe: Adobe got it right... Pin
dandy721-Sep-19 4:05
dandy721-Sep-19 4:05 
GeneralRe: Adobe got it right... Pin
kalberts31-Aug-19 12:15
kalberts31-Aug-19 12:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.