d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Building A Makeshift Cache > Need Efficiency Advise
Add Reply New Topic New Poll
Member
Posts: 3,752
Joined: Nov 4 2004
Gold: 0.81
Nov 4 2013 07:42pm
EDIT ** This is in Java.

Simple background:
I'm writing a program that parses a file and writes data to a server via http requests. I want to create a caching system that tracks everything I do so if necessary, i can undo what was done.

What is more efficient?

Using StringBuilder to create some string that contains the information, then writing to a file after the script has run, or
writing the cache using JSONObject/JSONArray, then writing to the file.

If there are better solutions than these 2 please let me know. I can't really think of anything better that would suit my task.

This post was edited by Xodius on Nov 4 2013 07:42pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 4 2013 08:06pm
it depends on what your data looks like and how you're doing everything. i assume the server has a DB on it. if everything is an insert, just store the primary keys. then to undo it, just delete the pkids. if you're including updates, then you'll have a difficult time of it.

if you know that you'll have to undo it immediately (eg if it fails), then just handle it via transactions and rollback. if you won't know till much later, i'd store it in another database table or add a column with details for however you're going to determine it needs to be undone. so if you run your program at 8pm and ends at 8:05 and generates 1000 new records, you can store a log that has the starting pkid and ending pkid and the time stamps. assuming nobody else can insert rows at the same time. on the other hand if it includes updates, you'll have to include all the original column values as well as which pkid changed. how to handle it most efficiently really depends on what you're doing.

if your question is specifically stringbuilder vs jsonobjects, then the string builder (or string buffer? whichever isn't thread safe) is likely faster. but that depends on how you go about formatting the data. for example do you already have jsonobjects and you're just gonna call tostring on them? on the other hand if you start with plain data and then you have to jsonify them to create json objects, obv that will take longer.

This post was edited by carteblanche on Nov 4 2013 08:13pm
Member
Posts: 3,752
Joined: Nov 4 2004
Gold: 0.81
Nov 5 2013 05:42pm
Thanks a lot. That was really helpful.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll