Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cache Records(Benefits)

 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have noticed people in past threads talking about cacher records and why they use them. I would like to have an extensive understanding of it's use! I obtained Max's book and i am kinda stuck with his concepts(And i dont see anyway in his book where he uses Cache records). Looking forward to your responses.)
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty sure Max doesn't specifically use a cache in his book... He does however load data from his database into a TableModel, which, in his implementation, more or less acts as a cache.

The basic benefit to a cache is faster reads and writes. The data (or a part of it) resides in memory instead of on disk, eliminating the need (or part of the need) to access persistent storage.

One disadvantage to using a cache in a multiple client environment is the possibility of stale data. Whenever a client acts on the data it has in its cache, it should perform some type of checking to ensure that the data has not been changed since it was loaded into the cache.

I have seen the assignment implemented both ways, and both ways have passed.
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speed is one case. In additional, you don't need to have db file open all the time (if file level lock is mandatory in the multiple access to the physical file, then this is crucial). In my passed implementation, a cache provides a meaningful locker object.

Stale data may not be a concern in this comparison. In this particular project requirement, modifiable operations must implement locking mechansim so no stale data should be (otherwise your lock fails). In nonmodifiable operations, dirty read/stale data is the same in both cache and non-cache mechanisms, because there is no sync to guarrantee it.
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used a cache for my data as well, and it really helped me out in the process of reading and writing. Another advantage to caching is that it prevents corrupting the data file if the file pointer is changed when it shouldn't be by different threads. Also, keep in mind that you should always update the data file first before updating the cache. Just think if thousands of clients had changed data kept in the cache but it wasn't written to the file yet. If the program crashed and the physical file wasn't updated, you probably would be in hiding from then on.
 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Thanks for your replies. However i have a question(might sound a bit stupid), how does one then implements a cache. I read some posts that said they loaded it into an ArrayList @ runtime and read and write data to it!I dont get it.( )!
One more question: I ran the code in Max's book(Interesting i must add), i howvever checked out the dvd_db and i found some files. I was wondering if that's how the db.db file comes with the assignment. I ran and found out the "Magic cookie" of the each record was the same. Anyways guys HELP OUT.
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used actually a couple of data structures: cachedRecords and deletedRecords which mapped to the nature of the data file. Here we use term, cache, because these data structures are persistent in memory through the life of application. In some other application, the cache may means a temp physical file. But the characteristic is the same: persistent existance of the data structure (not the data per se) in term of the life of the application.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saheed,

One more question: I ran the code in Max's book(Interesting i must add), i howvever checked out the dvd_db and i found some files. I was wondering if that's how the db.db file comes with the assignment.


In the assignment you will get a single data file, which contains all the records you work with.

In the assignment you will have to develop the code to read and write the records in this file - in Max's book each file is a serialized object.

As you read through the book you will find other differences as well ... In developing sample projects, there is always the dilemma of creating a project that meets many of the requirements set forth by the real SCJD project, while not actually giving away the test solution. This is actually quite a difficult task – it is very easy to develop a replica of the assignment you will receive from Sun, and discuss how to solve it’s problems. However tempting that might be for some people, we know that most SCJD candidates really want to go through the challenges of the real project themselves, and thus derive a greater sense of achievement when they complete it. Therefore the project in the book covers most of the same issues as the real assignment, while remaining different enough that you will not be able to copy and paste code into your assignment. While some parts are simpler than the real assignment, others are far more complex. Therefore anyone attempting to use the provided code as a base will need to really understand the code in order to determine which parts can be removed, not to mention which parts need to be added.

I ran and found out the "Magic cookie" of the each record was the same.




As Max's book uses serialization, he does not rely on magic cookies.

The JVM itself does use cookies. You can identify a Java class by the first four bytes:



The first four bytes (CAFE BABE) are the 'magic cookie' that identifies a Java Class.

Similarly you can look at the magic cookie for a serialized class:



But that doesn't really tell us much. Of much more interest is the serial version ID:



Anyways guys HELP OUT.


Do you feel that you aren't getting answers to your questions? If so, perhaps you could rephrase the question. In any case, please don't shout.

Regards, Andrew
 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops Didnt mean to shout! Sorry! Anyways thanks alot for your insights! I really appreciate them. I got all answers to my questions. Thanks once again.
 
David Fishman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been hanging around this forum for a few days now and would just like to express many thanks to all the dedicated people sharing their thoughts and experiences !
Back to the topic at hand - from what I've read so far about the "spirit" and intentions of the SCJD, I would tend to think that implementing a cache is more than little out of scope. To be sure there are performance and maybe even integrity benefits, but as far as I can see this is not telling the markers much in terms of the candidate's object-oriented abilities using J2SE !
I know that caching can be a tricky business, and in fact I applaude those who actually tackled it, but to be honest it looks more like a "technical manipulation" than central design choice for this type of assignment.
 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, as I found caching all my records into an ArrayList a hell of a lot easier than reading and writing to the file everytime an action is taken. All you need to do is create a class as a Record object that holds all the record information, and just update that. I find that a lot simpler than keeping track of a file pointer, but I guess everyone has their own way and opinions on doing it.
In the end, the best answer is: Do whatever meets the requirements and works.
 
kri shan
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, keep in mind that you should always update the data file first before updating the cache. Just think if thousands of clients had changed data kept in the cache but it wasn't written to the file yet. If the program crashed and the physical file wasn't updated, you probably would be in hiding from then on.
If i change anything on the data file, how cache knows something changed on the data file? whether everytime cache checks with the data file for any added/modified/deletd data?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic