1

如何在我的 Ehcache 周围环绕一个 try catch 以确保它已正确启动?

4

1 回答 1

2

您可以编写使用 CacheManager 检查缓存状态的包装方法,例如,

/**
 * 
 * @return true if Caching system is live otherwise false
 */
public boolean isAlive()
{
    return net.sf.ehcache.Status.STATUS_ALIVE == cacheManager.getStatus();
}

您始终可以将缓存调用包装为

public Object getVal(Object aKey, Object aDefaultValue)
{
    Element element = null;

    if (Util.isAlive())
    {
        try
        {
            element = cache.get(aKey);
        }
        catch (IllegalStateException e)
        {
            //Log it
        }
        catch (RuntimeException r)
        {
            //Log it
        }
    }

    return ((element == null) ? aDefaultValue : element.getObjectValue());
}

希望这可以帮助

于 2012-05-09T18:26:52.313 回答