Click here to Skip to main content
14,456,907 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 2:17
MemberGary Wheeler3-Sep-19 2:17 
GeneralRe: A Programming Philosophy Question Pin
honey the codewitch31-Aug-19 18:45
mvahoney the codewitch31-Aug-19 18:45 
GeneralRe: A Programming Philosophy Question Pin
Grober_1-Sep-19 0:02
MemberGrober_1-Sep-19 0:02 
GeneralRe: A Programming Philosophy Question Pin
Chris Losinger1-Sep-19 4:34
professionalChris Losinger1-Sep-19 4:34 
GeneralRe: A Programming Philosophy Question Pin
KateAshman1-Sep-19 22:39
MemberKateAshman1-Sep-19 22:39 
GeneralRe: A Programming Philosophy Question Pin
Richard Andrew x642-Sep-19 5:07
professionalRichard Andrew x642-Sep-19 5:07 
GeneralRe: A Programming Philosophy Question Pin
NotJosh2-Sep-19 20:18
MemberNotJosh2-Sep-19 20:18 
GeneralHappy coding. Pin
honey the codewitch31-Aug-19 13:28
mvahoney the codewitch31-Aug-19 13: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

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

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 10:54
communityengineerJörgen Andersson31-Aug-19 10:54 
GeneralRe: Seems about right Pin
OriginalGriff31-Aug-19 20:49
communityengineerOriginalGriff31-Aug-19 20:49 
GeneralRe: Seems about right Pin
Jörgen Andersson31-Aug-19 22:42
communityengineerJörgen Andersson31-Aug-19 22:42 
GeneralRe: Seems about right Pin
OriginalGriff31-Aug-19 22:57
communityengineerOriginalGriff31-Aug-19 22:57 
GeneralRe: Seems about right Pin
Jörgen Andersson31-Aug-19 23:13
communityengineerJörgen Andersson31-Aug-19 23:13 
GeneralRe: Seems about right Pin
OriginalGriff31-Aug-19 23:42
communityengineerOriginalGriff31-Aug-19 23:42 
GeneralRe: Seems about right Pin
Sander Rossel1-Sep-19 0:01
professionalSander Rossel1-Sep-19 0:01 
GeneralRe: Seems about right Pin
Gary Wheeler3-Sep-19 2:20
MemberGary Wheeler3-Sep-19 2:20 
GeneralPhoto's - Remembering the battle of the Scheldt Pin
RickZeeland31-Aug-19 3:11
communityengineerRickZeeland31-Aug-19 3:11 
GeneralRe: Photo's - Remembering the battle of the Scheldt Pin
Richard MacCutchan31-Aug-19 5:57
communityengineerRichard MacCutchan31-Aug-19 5:57 
GeneralAdobe got it right... Pin
Sander Rossel31-Aug-19 1:50
professionalSander Rossel31-Aug-19 1:50 
GeneralRe: Adobe got it right... Pin
Ron Anders31-Aug-19 3:31
MemberRon Anders31-Aug-19 3:31 
GeneralRe: Adobe got it right... Pin
Mike Hankey31-Aug-19 5:50
professionalMike Hankey31-Aug-19 5:50 
GeneralRe: Adobe got it right... Pin
dandy7231-Aug-19 8:26
Memberdandy7231-Aug-19 8:26 
GeneralRe: Adobe got it right... Pin
Mike Hankey31-Aug-19 15:59
professionalMike Hankey31-Aug-19 15:59 
GeneralRe: Adobe got it right... Pin
dandy721-Sep-19 5:05
Memberdandy721-Sep-19 5:05 
GeneralRe: Adobe got it right... Pin
Member 798912231-Aug-19 13:15
MemberMember 798912231-Aug-19 13: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.