Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help: Bug with removing data from a Hash Map

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help: Bug with removing data from a Hash Map

    Hi,

    I am working on this code..where a datacache is implemented as a HashMap. The Hashmap has a key and an string arraylist <> associated with it. There is a refresh time period and data expiry time associated with data in the hashmap. In certain scenerios, some data of certain Type in the data cache needs to be refreshed and not all.

    The way refresh is working currently, it makes a query to the database for the dataType which needs to be refershed and adds that data to the cache. Since the key for HashMap needs to be unique it overwriites any data associated with a key which already exists and adds any new data.

    However, a bug with this implemention is it does not remove any data from the cache which does not exsist in the database anymore.

    Nothing gets removed from the datacache until expiry period is reached. But it is not necessary refresh will always be called after expiry of data.

    If someone can please suggest a way I can improve the refresh method so that it removes any Key/data from the cache which is not in the database anymore?

    I do not want to clear the cache completely before refresh is called.

    Any suggestions?

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help: Bug with removing data from a Hash Map

    does not remove any data from the cache
    This sounds like a logic problem. Hard to make any suggestions without seeing the code.
    Does the code that is to remove the entries from the HashMap get called when you want it to be called?

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help: Bug with removing data from a Hash Map

    Caches have to be read-through, write-through or they just don't work. If you have some other access to the backing store which is modifying it independently of the cache, the only way you can fix this problem is to check the backing store to see if your cached data is stale, and bang goes your cache benefit (okay, you save some deserialization).

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help: Bug with removing data from a Hash Map

    Quote Originally Posted by Norm View Post
    This sounds like a logic problem. Hard to make any suggestions without seeing the code.
    Does the code that is to remove the entries from the HashMap get called when you want it to be called?
    Yes, the code to remove the entries can be called at anytime. However, the way the remove function is implemented right now it removes only the data which has reached its expiry time. It does not check for data which may not be in the database anymore.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help: Bug with removing data from a Hash Map

    Can't you just remove the data from the Map when it is removed from the database? Without knowing more about the implementation its hard to suggest specifics, but given a simple Map object, when delete is called, just call remove on the map with the given key.

Similar Threads

  1. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM
  2. [SOLVED] MD5 hash and MimeMessage problem
    By ArgyrisV in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 19th, 2011, 04:28 PM
  3. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM
  4. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM