Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Implement Simple Threadsafe Cache using HashMap without using Synchronized Collection

$
0
0

Java In Memory Cache using Hashmap Implement Simple Threadsafe Cache using HashMap without using Synchronized Collection

A cache is an area of local memory that holds a copy of frequently accessed data that is otherwise expensive to get or compute. Examples of such data include a result of a query to a database, a disk file or a report.

Here is a simple Java Example which is Threadsafe using HashMap without using Synchronized Collections.

package com.ebay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

// Create Simple Cache object with the help of HashMap...
public class CrunchifyCacheExample {

	private long timeToLive;
	private HashMap<String, String> cacheMap;

	protected class CrunchifyCacheObject {
		public long lastAccessed = System.currentTimeMillis();
		public String value;

		protected CrunchifyCacheObject(String value) {
			this.value = value;
		}
	}

	public CrunchifyCacheExample(long timeToLive, final long timeInterval, int max) {
		this.timeToLive = timeToLive * 2000;

		cacheMap = new HashMap<String, String>();

		if (timeToLive > 0 && timeInterval > 0) {

			Thread t = new Thread(new Runnable() {
				public void run() {
					while (true) {
						try {
							Thread.sleep(timeInterval * 1000);
						} catch (InterruptedException ex) {
						}

					}
				}
			});

			t.setDaemon(true);
			t.start();
		}
	}

	// PUT method
	public void put(String key, String value) {
		synchronized (cacheMap) {
			cacheMap.put(key, value);
		}
	}

	// GET method
	public String get(String key) {
		synchronized (cacheMap) {
			String c = cacheMap.get(key);

			if (c == null)
				return null;
			else {
				return c;
			}
		}
	}

	// REMOVE method
	public void remove(String key) {
		synchronized (cacheMap) {
			cacheMap.remove(key);
		}
	}

	// Get Cache Objects Size()
	public int size() {
		synchronized (cacheMap) {
			return cacheMap.size();
		}
	}

	// CLEANUP method
	public void cleanup() {

		long now = System.currentTimeMillis();
		ArrayList<String> deleteKey = null;

		synchronized (cacheMap) {
			Iterator<?> itr = cacheMap.entrySet().iterator();

			deleteKey = new ArrayList<String>((cacheMap.size() / 2) + 1);
			CrunchifyCacheObject c = null;

			while (itr.hasNext()) {
				String key = (String) itr.next();
				c = (CrunchifyCacheObject) ((Entry<?, ?>) itr).getValue();
				if (c != null && (now > (timeToLive + c.lastAccessed))) {
					deleteKey.add(key);
				}
			}
		}

		for (String key : deleteKey) {
			synchronized (cacheMap) {
				cacheMap.remove(key);
			}

			Thread.yield();
		}
	}
}

Some more Java Examples which you may want to look.

Bonus: Java Memory Model Details:

Java Memory Model Implement Simple Threadsafe Cache using HashMap without using Synchronized Collection

The post Implement Simple Threadsafe Cache using HashMap without using Synchronized Collection appeared first on Crunchify.
Author: Arpit Shah.


Viewing all articles
Browse latest Browse all 1037

Trending Articles