FileSystemCacheBackend implements ICacheBackend { @Override public boolean purge( final List<String> key ) { final List<String> cleanKey = sanitizeKeySegments( key ); final List<Lock> locks = lockForWrite( cleanKey ); try { final String fileName = cachePath + StringUtils.join( cleanKey, File.separator ); if ( fileName.endsWith( DATA ) ) { final File data = new File( fileName ); if ( !data.exists() ) { return true; } final File meta = new File( fileName.replace( DATA, METADATA ) ); syncMap.remove( cleanKey ); return data.delete() && meta.delete(); } final File file = new File( fileName ); if ( file.isDirectory() ) { final Set<String> subKeys = listKeys( cleanKey ); for ( final String subKey : subKeys ) { final ArrayList<String> subEntry = new ArrayList<>( cleanKey ); subEntry.add( subKey ); purge( subEntry ); } syncMap.remove( cleanKey ); FileUtils.deleteDirectory( file ); return !file.exists(); } final File data = new File( fileName + DATA ); if ( !file.exists() && !data.exists() ) { return true; } final File metadata = new File( fileName + METADATA ); syncMap.remove( cleanKey ); return data.delete() && metadata.delete(); } catch ( final Exception e ) { logger.debug( "Can't delete cache: ", e ); return false; } finally { unlock( locks ); } } FileSystemCacheBackend(); void setCachePath( final String cachePath ); @Override boolean write( final List<String> key, final Serializable value,
                        final Map<String, Serializable> metaData ); @Override Serializable read( final List<String> key ); Map<String, Serializable> readMetaData( final List<String> key ); void purgeSegment( final List<String> key,
                            final BiPredicate<List<String>, Map<String, Serializable>> p ); @Override boolean purge( final List<String> key ); static final String REPLACEMENT; static final String SLASHES; static final String EXT; static final String DATA; static final String METADATA; }
@Test public void testPurge() throws Exception { assertTrue( fileSystemCacheBackend.write( Arrays.asList( directoryKey, key ), value, new HashMap<String, Serializable>() ) ); assertEquals( fileSystemCacheBackend.read( Arrays.asList( directoryKey, key ) ), value ); assertTrue( fileSystemCacheBackend.purge( Arrays.asList( directoryKey, key ) ) ); assertNull( fileSystemCacheBackend.read( Arrays.asList( directoryKey, key ) ) ); }