• How to purge an item from the cache (MediaCache)

    This article describes how to use Java API methods to instruct your Wowza media server to purge on-demand assets from the cache when using the Media Cache feature. You can purge an asset from the cache if it's not being referenced or played by clients. Later versions of Wowza Streaming Engine software support purging assets that are still in use.

    Note: For more information about how to use and extend the Java API methods, see How to extend Wowza Streaming Engine using Java.

    Purge assets not in use

    Use the following Java API methods to purge an asset from the cache if it's not being referenced or played by clients.

    To purge an asset from the cache based on the asset mediaName, which is the asset stream name minus the prefix and after any play aliasing, use the following call:
    MediaCache mediaCache = MediaCacheImpl.getMediaCache();
    mediaCache.flushItemFromCache(mediaName);
    You can also use the following call that includes the asset stream name with the prefix. This will take the stream name (with the prefix) and run it through an aliasing system to resolve it to the proper media name, and then purge the asset from the cache:
    MediaCache mediaCache = MediaCacheImpl.getMediaCache();
    mediaCache.flushStreamNameFromCache(appInstance, streamName);

    Purge assets in use

    Note: These methods are only supported in Wowza Streaming Engine™ 4.3 and later.
    Use the following Java API methods to purge an asset from the cache even if it's still being referenced or played by clients.

    To purge an asset from the cache based on the asset mediaName, which is the asset stream name minus the prefix and after any play aliasing, use the following call:
    MediaCache mediaCache = MediaCacheImpl.getMediaCache();
    mediaCache.flushAndForceItemFromCache(mediaName);
    You can also use the following call that includes the asset stream name with the prefix. This will take the stream name (with the prefix) and run it through an aliasing system to resolve it to the proper media name, and then purge the asset from the cache:
    MediaCache mediaCache = MediaCacheImpl.getMediaCache();
    mediaCache.flushAndForceStreamNameFromCache(appInstance, streamName);
    These methods purge the specified asset 30 seconds after the last currently connected client is disconnected. After the call is made, Media Cache will disconnect all currently connected clients, purge the asset from the cache, and then, if a new request is made for the asset, get new copy of the asset from the source. These calls are useful if you want to remove a specific asset and/or remove an asset so it can be refreshed when a new client requests it.

    The following example HTTP Provider shows how you could use the flushAndForceItemFromCache method:
    package com.wowza.demo.mediacache.gc;
    
    import java.io.*;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import com.wowza.wms.http.*;
    import com.wowza.wms.logging.*;
    import com.wowza.wms.mediacache.impl.MediaCacheImpl;
    import com.wowza.wms.mediacache.model.MediaCache;
    import com.wowza.wms.vhost.*;
    
    public class HTTPForceDelete extends HTTProvider2Base {
    
    	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
    		if (!doHTTPAuthentication(vhost, req, resp))
    			return;
    
    		Map<String, List<String>> params = req.getParameterMap();
    		String itemName ="";
    		
    		if ( params.containsKey("itemname") )
    			itemName = params.get("itemname").get(0);
    
    		boolean foundItem = false;
    		if ( itemName.length() > 0 ) {
    			MediaCache mediaCache = MediaCacheImpl.getMediaCache();
    			Iterator<String> itemNamesI = mediaCache.cacheItemNames().iterator();
    			while (itemNamesI.hasNext()) {
    					String thisItem = itemNamesI.next();
    					WMSLoggerFactory.getLogger(HTTPForceDelete.class).info("Item in list was called "+thisItem);
    					if ( thisItem.endsWith(itemName) ) {
    						foundItem = mediaCache.flushAndForceItemFromCache(thisItem);
    					}
    				}
    		}
    		  
    		String retStr  = "Item "+itemName+" ";
    		if ( foundItem == true )
    			retStr += "found.
    ";
    		else
    			retStr += "NOT FOUND.
    ";
    		
    		try {
    			OutputStream out = resp.getOutputStream();
    			byte[] outBytes = retStr.getBytes();
    			out.write(outBytes);
    		}
    		catch (Exception e) {
    			WMSLoggerFactory.getLogger(null).error("HTTPForceDelete: " + e.toString());
    		}
    	}
    }

    Originally Published: 02-22-2011.
    Updated: For Wowza Streaming Engine 4.3.0 on 10-06-2015.

    If you're having problems or want to discuss this article, post in our forum.