Am I clever or stupid?

I came up with something at work yesterday that I can’t tell if it’s extremely clever, or way too complicated.

The application has these objects, called Playables and other objects called Playlists. A single Playable can be in dozens of Playlists, as well as appearing in several JTables on different parts of the GUI. The basis of the Playable is a database table which other applications can update at various times, and then send a message telling the GUI to update itself.

As an old C/C++ hand, I’m well aware of the fact that while Java tries its damnedest to hide it, when you put an Object into a Collection, it really just stores a pointer to it.

And this leads to my first optimization – when I want a Playable of a certain type, I go to what I call a “first level cache”, and if it’s there I get it from the cache. That means that all these different places that refer to the same Playable get (a pointer to) the same Object. I’ve been doing that for a while, and things were pretty good at updating when one part of the GUI changed it. The problem came with finding a simple and elegant way to make it reread stuff from the database when things were changed from outside. The original code ended up reloading everything from the database on any change notification.

So here’s where inspiration struck. Now, when I get a change notification, I move everything from the “first level cache” to a “second level cache”. Then when somebody asks for the Playable, the “getFromCache” method doesn’t return anything, so the program gets it from the database and builds a new Playable. But when it calls “putToCache”, it grabs the one from the second level cache, and replaces all the class variables with what came out of the database, and moves it from the second level cache to the primary cache. Whenever anything does a full query on everything in the database, it does the whole thing – moves the primary cache to the secondary, gets all the Playables out of the database, updates the Objects, and then cleans out the secondary cache.

The only problem still remaining is making sure something requests the ones that have updated. Some updates I do a full query and replace, and other updates attempt to notify what exactly has changed.

Work continues apace.

One thought on “Am I clever or stupid?”

Comments are closed.