Posts

Simple in memory cache with plain Java

The following example is simple in memory cache using plain java without using any third party cache libraries. The below implementation shows expiry time based cache eviction and setting maximum cache size. If used with Spring Framework, it is ideal to make this class as DisposableBean to safely execute the shutdown method during application shutdown event. As below @Component public class SimpleCache implements DisposableBean { @Override public void destroy () throws Exception { shutdown(); } } Full implementation: import java.time.Duration ; import java.time.Instant ; import java.util.Comparator ; import java.util.Objects ; import java.util.UUID ; import java.util.concurrent.ConcurrentHashMap ; import java.util.concurrent.Executors ; import java.util.concurrent.ScheduledExecutorService ; import java.util.concurrent.TimeUnit ; public class SimpleCache { public record HeavyObject ( UUID id , String firstName , String lastName ) { } private record CacheValue (...

Unix Essential Commands

chmod command (change a file mode) Symbol and its meaning u -> user g -> group o -> others a -> all r -> read w -> write (and delete) x -> execute (and access directory) + -> add permission - -> take away/remove permission  $ chmod go-rwx biglist To remove read write and execute permissions on the file/directory biglist for the group and others $ chmod a+rw biglist To give read and write permissions on the file/directory biglist to all, $ chmod u+rwx,g+rw,o-rwx test.file Set multiple permission in single command $ chmod a+rwx directory/* To give permission to all files in a directory $ chmod -R a+rwx directory/* To give permission to all files/directories in a directory and its sub directories cut command printf "Hello\tworld\nHi\tthere\n" | cut -f 2 Outputs the 2nd element after splitting the tab separated columns. -f value starts from 1 echo 'key=value' | cut -d "=" -f 2 Outputs the 2nd element after splitting the st...

awk - unix

Simple Examples Extract and print a first column from the input ps | awk '{print $1}' Extract and print a multiple columns from the input with space between each column ps | awk '{print $1, $3}' Extract and print a multiple columns from the input without space ps | awk '{print $1$3}' Extract and print a multiple columns from the input with various formatting ps | awk '{print $1">>"$2"\t"$3}' Extract and print multiple on new lines ps | awk '{print $1; print $2;}' Extract and print a columns without newline at the end ps | awk '{printf $1">>"$2">>"$3"**"}' BEGIN block This block is executed only once before processing first row in the input ps | awk 'BEGIN { print "Header" } {print $1}' ps | awk 'BEGIN { print "Header"; print "SubHeader"; } {print $1}' END block This block is executed only once before processing first row in th...