• How to control item expiry (Media Cache)

    This article describes how to control items that are about to expire from the Media Cache. You can control if an item is kept in the cache or allowed to expire by implementing an expiry class and a specific configuration item that points to the class in the Media Cache base configuration.

    Note: Wowza Streaming Engine™ 4.3.0 or later is required.

    Creating an expiry class


    To implement an expiry class, you must install Eclipse and the Wowza IDE. For more information, see How to extend Wowza Streaming Engine using the Wowza IDE.

    The code example below allows all items to be expired. However, to leave an item in the cache, set setItemRemove to false each time the Media Cache item check is called. IMediaCacheStoreItemListener must be used for proper implementation.
    package com.wowza.demo.mediacache;
    
    import com.wowza.wms.logging.WMSLoggerFactory;
    import com.wowza.wms.mediacache.model.IMediaCacheStoreItemEvent;
    import com.wowza.wms.mediacache.model.IMediaCacheStoreItemListener;
    
    public class Expiry implements IMediaCacheStoreItemListener
    {
    
    	public void init()
    	{		
    	}
    
    	public void processMediaCacheItemEvent(IMediaCacheStoreItemEvent itemEvent)
    	{
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemName '"+itemEvent.getItemName()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemSize '"+itemEvent.getItemSize()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemNumber '"+itemEvent.getItemNumber()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemTotalCount '"+itemEvent.getItemTotalCount()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter freed space '"+itemEvent.getFreedSpaceTotal()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter badItemTime '"+itemEvent.getBadItemTime()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemStorePath '"+itemEvent.getItemStorePath()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemReleaseTime '"+itemEvent.getReleaseItemTime()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemMaxTTL '"+itemEvent.getMaxTTLReached()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemMinTTL '"+itemEvent.getMinTTLReached()+"'");
    		WMSLoggerFactory.getLogger(Expiry.class).info("itemEvent parameter itemRemove '"+itemEvent.getItemRemove()+"'");	
    		// You can now perform operations on the item, for example not allow the item to expire
    		itemEvent.setItemRemove(false);				
    	}
    }

    Configuring MediaCache.xml


    You can't add controls for an item in Wowza Streaming Engine Manager. You must do this by editing the [install-dir]/conf/MediaCache.xml file in a text editor.

    To add item expiry control:

    1. Use a text editor to open the [install-dir]/conf/MediaCache.xml file and add the <CacheStoreItemListenerClassList> property to the <MediaCache> container at the top of the file. Make sure this definition contains the full class name of the item expiry class you want to use. For example:
      <CacheStoreItemListenerClassList>com.wowza.demo.mediacache.Expiry</CacheStoreItemListenerClassList>
      You can invoke multiple expiry classes one after another to determine if item expiry should be performed by adding them as a comma-separated list.

    2. Save MediaCache.xml and restart the Wowza media server software to apply the changes.

    The <MediaCache> container will look like the following example:
    <MediaCache>
    	<WriterThreadPool>
    		<PoolSize>${com.wowza.wms.TuningAuto}</PoolSize>
    	</WriterThreadPool>
    	<ReadAheadThreadPool>
    		<PoolSize>${com.wowza.wms.TuningAuto}</PoolSize>
    	</ReadAheadThreadPool>
    	<MaxPendingWriteRequestSize>${com.wowza.wms.TuningAuto}</MaxPendingWriteRequestSize>
    	<MaxPendingReadAheadRequestSize>${com.wowza.wms.TuningAuto}</MaxPendingReadAheadRequestSize>
    	<GCFrequency>10000</GCFrequency>
    	<ContextMapperClass></ContextMapperClass>
    	<AddFileExtensionIfNeeded>true</AddFileExtensionIfNeeded>
    	<URLEscapeStreamNameSpaces>true</URLEscapeStreamNameSpaces>
    	<URLEscapeStreamNameAll>false</URLEscapeStreamNameAll>
    	<OnStartReloadCache>false</OnStartReloadCache>
    	<OnStartReloadCacheVerifySource>true</OnStartReloadCacheVerifySource>
    	<CacheStoreItemListenerClassList>com.wowza.demo.mediacache.Expiry</CacheStoreItemListenerClassList>
    	<DebugLog>false</DebugLog>
    </MediaCache>

    Originally Published: For Wowza Streaming Engine 4.3.0 on 01-11-2016.

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