@ SuppressWarnings ( "unchecked" ) public < T extends VALUE > T withCaching ( KEY key , Supplier < VALUE > cacheableOperation ) { Assert . notNull ( key , "Key is required" ) ; Assert . notNull ( cacheableOperation , "Supplier is required" ) ; ReadWriteLock lock = getLock ( ) ; return ( T ) Optional . ofNullable ( read ( lock , key ) ) . orElseGet ( ( ) -> Optional . ofNullable ( cacheableOperation . get ( ) ) . map ( value -> write ( lock , key , value ) ) . orElse ( null ) ) ; }
Implementation of the look - aside cache pattern .