Showing posts from September, 2018

Building an Efficient Cache

building-an-efficient-cacheTo build a cache could be as simple as to just to use a key-value pair kind data structure. It works sufficient most of the time in a single-threaded application. However, when there are many clients access the same cache while the computation being conducted can yield duplicated caching attempts thus defeat the purpose of caching, which is to avoid duplicated computation. This article introduces the existing caching patterns and discusses the key points to create an efficient cache.Define the Cache BehaviorLet’s define an interface that represents the core behavior of the caches we build. Computable is a functional interface that only has one abstract method compute() that throws an InterruptedException. This exception might be thrown when any synchronization failure happens.publicinterfaceComputable<T, R>{ R compute(T t)throws InterruptedException;}SimpleCacheMap interface defines a key-value pair kind data structure that could be used to interna…

Basic Serialization

basic-serializationSerialization includes the part that serializes an object to a file and deserializes a file to an object that has the desired same state as it was in. It is typically done by using two high-level classes from the package - ObjectOutputStream and ObjectInputStream. In this article, we demonstrate the basic operations of serializing and deserializing.Key Points in SerializationA class must implement Serializable interface before its object can be serialized.If a class implements Serializable, all its instances variables that have a reference to another object must be transientor the object referenced to must also implement Serializable.If an instance variable is marked transient, it will not be serialized.private void writeObject(ObjectOutputStream oos) and private void readObject(ObjectInputStream ois) could be used to manually add additional logic to the serialization process. This could be very useful to serialize objects that don’t implement Serializable.o…