0

I am developing an ordering system and here is my scenario: I have a class that holds the new orders in such like a "caching" strategy. At this time, I am using a HashMap like this:

public static final Map<Long, Order> orders = new HashMap<Long, Order>();

Where I identify an order uniquely by a long (that doesn't come from a DB, it's just an incremented variable). The reason I don't save it from DB is that for example: the employee is registering an order coming from a phone call and suddenly the customer doesn't want to order anymore, so that I won't populate my DB with an unuseful register. Based on that, I'm looking for an approach to hold all orders and "clean automatically" the indexes that are not being used anymore.

I've looked at WeakReference but I don't know if that's what I really need. I'm open to any suggestion.

Thanks in advance.

lucasdc
  • 1,032
  • 2
  • 20
  • 42
  • Why wouldn't you just hold the details in an object as the employee is entering them, and only save them all to the database when the customer has confirmed the order? In either case, the `long` id sounds like it should be a key on the database layer, not something you decide on the Java layer (if you specify AUTO_INCREMENT on your id on the db, you get the guarantee of consistency for free.) – Michael Berry May 07 '15 at 01:14
  • I would recommend using an ORM tool like Hibernate. You can save `Order`s into the database when they are confirmed. – Tim Biegeleisen May 07 '15 at 01:17
  • because: 1) there might be more than one employee registering orders at the same time; 2) I am acessing this object by other classes, so that, I need to have something to identify each order uniquely I think saving it to DB is not viable, because every time an user opens the screen to register an order, a new order is created. So, if user "misclick" there would be another unuseful register in DB... – lucasdc May 07 '15 at 01:22
  • 1
    Actually, I am using Hibernate @TimBiegeleisen – lucasdc May 07 '15 at 01:23

1 Answers1

2

You don't want a WeakReference for this. A WeakReference is used in the case where you are caching large objects in memory (say an image), and you want to indicate it is OK for the Garbage Collector to remove this image if free memory becomes sparse. In this case you can always reload the image from disk later on, so you don't care if it is removed from memory.

For your situation, I would use a plain HashMap. Once the customer has confirmed they actually want the order, then you can persist it to the database.

If the customer doesn't want the order, then simply remove it from the HashMap - a very inexpensive operation.

EDIT:

You may also be looking for a "Timed Cache". Something like a HashMap that auto-expires any key/value after a certain amount of time. Google's Guava has a data structure like this. See related SO question.

Community
  • 1
  • 1
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • "If the customer doesn't want the order, then simply remove it from the HashMap". The problem with this is that I cannot always know when the order will not be completed - as I said in my comment above - "because every time an user opens the screen to register an order, a new order is created." So, if employee 'misclick' and then attempt to access another page, I wouldn't know that this order would not be completed. – lucasdc May 07 '15 at 02:00
  • If the employee mis-click, and create a new order, can they go back to the old one? If yes, then your best bet is to simply time out the old order after some amount of time. – Martin Konecny May 07 '15 at 02:03
  • The "misclicked order" would be lost in space :). How could I time out the order? Is there any way to do this without something like opening a thread and checking every X time if a variable is set nor using 3rd party libraries (such as Guava)? – lucasdc May 07 '15 at 02:08
  • If the employee mis-clicks, and a new order is created, why can't you use this situation to immediately discard the old order? – Martin Konecny May 07 '15 at 02:10
  • Let me try to explain with an use case: Employee attempts to access "Profile" page but mis-clicks on page "Orders". When he clicks, my servlet creates a new order and adds it into my HashMap. After that, user clicks on "Profile" page. The order created will not be acessible anymore because I don't have it's id. This is a case where I would want to remove the order from my HashMap – lucasdc May 07 '15 at 02:15
  • 2
    Why don't you create a temporary data structure that has the same longetivity as your "Orders" page. When you leave the Order's page you are discarding all objects that created that view, so would also remove this data structure. The data structure only gets merged into your main `HashMap` when the order is actually completed. – Martin Konecny May 07 '15 at 02:17