Persistences Caching - File Serializable

The basics

If your Value is serializable, we can use "ObjectOutputStream" and "ObjectInputStream" to store and retrieve the value with a file.

There are three ways to do this.
Type Key Type Filename Instance class Description
Filename String The string key value. DefaultFilenameSerializableFileValueManager
<V extends Serializable>
Serializes the value to a file where the filename is the key string value.
The value V must implement Serializable.

Indexed K extends Serializable Random UUID DefaultIndexedSerializableFileValueManager
<K extends Serializable,V extends Serializable>
Serializes the value to a file where the filename is a random UUID,
the key to file map is stored in a seperate index file which is also persisted frequently.
Both the key (K) and value (V) must extend Serializable.
Think hard what a good "indexSaveInterval" is, and test it.

Keyed K extends Keyed<K> Random UUID DefaultKeyedSerializableFileValueManager
<K extends Serializable,V extends Keyed<K> & Serializable>
Serializes the value to a file where the filename is a random UUID,
the key stored inside the value using the Keyed interface.
Both the key (K) and value (V) must extend Serializable.
The value (V) must also extend the Keyed interface.


Construction examples

CachingDirectory cachingDirectory= new CachingDirectory(Files.createTempDirectory("serstorage").toFile());
LRUCachingMapConfiguration configuration = DefaultLRUCachingMapConfiguration.ONE_DAY();
And the Book.class defined at the bottom of this page.

Construction - DefaultFilenameSerializableFileValueManager

//Manual
DefaultFilenameSerializableFileValueManager<Book> valueManager = new DefaultFilenameSerializableFileValueManager<Book>(cachingDirectory,PersistencesRestoreMethod.DELETE_ALL);
DefaultFileLRUCachingMap<String,Book> cache = new DefaultFileLRUCachingMap<String, Book>(configuration,valueManager);

//Factory
cache = LRUCachingMapFactory.createFilenameSerializedCache(configuration, cachingDirectory,PersistencesRestoreMethod.NOTHING);

Construction - DefaultIndexedSerializableFileValueManager

//Manual
DefaultIndexedSerializableFileValueManager<String,Book> valueManager = new DefaultIndexedSerializableFileValueManager<String,Book>(cachingDirectory,PersistencesRestoreMethod.LOAD_ALL,10);
DefaultFileLRUCachingMap<String,Book> cache = new DefaultFileLRUCachingMap<String, Book>(configuration,valueManager);

//Factory
cache = LRUCachingMapFactory.createIndexedSerializedCache(configuration, cachingDirectory,PersistencesRestoreMethod.DELETE_ALL,10);

Construction - DefaultKeyedSerializableFileValueManager

//Manual
DefaultKeyedSerializableFileValueManager<String,Book> valueManager = new DefaultKeyedSerializableFileValueManager<String,Book>(cachingDirectory,PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);
DefaultFileLRUCachingMap<String,Book> cache = new DefaultFileLRUCachingMap<String, Book>(configuration,valueManager);

//Factory
cache = LRUCachingMapFactory.createKeyedSerializedCache(configuration, cachingDirectory,PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);

Example class - Book

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Book implements Serializable,Keyed<String> {

	private static final long serialVersionUID = 1411270976129358371L;
	//
	private String id;
	private String title;
	private Double price;
	private String author;
	private Date releaseDate;
	private String description;
	private String genre;

	public Book() {
		super();
	}

	@Override
	public String getKey() {
		return getId();
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getReleaseDate() {
		return releaseDate;
	}

	public void setReleaseDate(Date releaseDate) {
		this.releaseDate = releaseDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getGenre() {
		return genre;
	}
	
	public void setGenre(String genre) {
		this.genre = genre;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((author == null) ? 0 : author.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((genre == null) ? 0 : genre.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((price == null) ? 0 : price.hashCode());
		result = prime * result
				+ ((releaseDate == null) ? 0 : releaseDate.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (author == null) {
			if (other.author != null)
				return false;
		} else if (!author.equals(other.author))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (genre == null) {
			if (other.genre != null)
				return false;
		} else if (!genre.equals(other.genre))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (price == null) {
			if (other.price != null)
				return false;
		} else if (!price.equals(other.price))
			return false;
		if (releaseDate == null) {
			if (other.releaseDate != null)
				return false;
		} else if (!releaseDate.equals(other.releaseDate))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}
}