0

我知道关于 SO 有很多 CCE 问题。我已经详细或简要地阅读了其中的大部分内容,但我找不到任何适用于我的情况的内容。我的确切错误是:

Exception in thread "pool-1-thread-1" java.lang.ClassCastException: datastructures.instances.JClass cannot be cast to java.util.ArrayListif ((results = mCallsDownstreamCache.get(origin)) == null) {

正如您将在代码中看到的那样,我正在做的是ArrayList从缓存(HashMap)中请求一个,然后对此做出决定。这里的奇怪行为datastructures.instances.JClass是在生成错误的代码段中没有引用。

为了给你一些背景信息,我有一个数据库“模型”,它满足来自“控制器”的请求。这些结果存储在模型本地的缓存中,如果它们存在,模型将返回缓存,因此不必访问数据库。我的缓存元素是Commons 的 JCS的有效装饰器。

有问题的行被包裹在一个block commentinline comment

public class AnalyzeModel extends Model {

    public final String TAG = getClass().getSimpleName();
    public CacheDecorator<Integer, JClass> mClassCache = new CacheDecorator<Integer, JClass>();
    public CacheDecorator<Integer, JMethod> mMethodCache = new CacheDecorator<Integer, JMethod>();
    public CacheDecorator<Integer, ArrayList<Integer>> mCallsUpstreamCache =
            new CacheDecorator<Integer, ArrayList<Integer>>();
    public CacheDecorator<Integer, ArrayList<Integer>> mCallsDownstreamCache =
            new CacheDecorator<Integer, ArrayList<Integer>>();

    public void close() {
        super.close();
    }

    public Pair<Integer, ArrayList<Integer>> selectCallGraphDownstream(int origin) {
        ArrayList<Integer> results = new ArrayList<Integer>();

        /**
         * This is the offending line
         */
        if ((results = mCallsDownstreamCache.get(origin)) == null) {
        // End error line
            results = new ArrayList<Integer>();
            for (Record r : mQuery.select(
                    mQuery.mQuerier.select(Calls.CALLS.TID)
                            .from(Calls.CALLS)
                            .where(Calls.CALLS.SID.eq(origin)))) {
                results.add(r.getValue(Calls.CALLS.TID));
            }
            mCallsDownstreamCache.put(origin, results);
            Statistics.CACHE_MISS++;
        } else {
            Statistics.CACHE_HITS++;
        }
        return new Pair<Integer, ArrayList<Integer>>(origin, results);
    }

}

public class CacheDecorator<K, V> {

    public final String TAG = getClass().getSimpleName();

    private CacheAccess<K, V> mCache;

    public CacheDecorator() {
        try {
            mCache = JCS.getInstance("default");
        } catch (CacheException e) {
            BillBoard.e(TAG, "Error getting cache configuration: " + e.toString());
            e.printStackTrace();
        }
    }

    /**
     * Get an object from cache
     * @param obj object to retrieve from cache
     * @return generic object of retrieved value, null if not found
     */
    public V get(K obj) {
        return mCache.get(obj);
    }

    /**
     * Place an object in cache
     * @param key generic key for reference
     * @param obj generic object to be cached
     */
    public synchronized void put(K key, V obj) {
        try {
            if(obj != null) {
                mCache.putSafe(key, obj);
            }
        } catch( CacheException e) {
            //BillBoard.d(TAG, obj.toString());
            //BillBoard.e(TAG, "Error placing item in cache: " + e.toString());
            //e.printStackTrace();
        }
    }

    /**
     * Get the stats from our cache manager
     * @return String of our cache object
     */
    public String getStats() {
        shutDownCache();
        return mCache.getStats();
    }

    public static void shutDownCache() {
        CompositeCacheManager.getInstance().shutDown();
    }
}

一些可能有用也可能没有帮助的额外细节:

  • Pair<V, K>数据结构只是一个不可变的 2 对元组类
  • CacheDecorator.get(V obj)null如果对象在缓存中不存在则返回
  • 我在选角等方面尝试了很多
  • JClass确实在代码的其他地方有引用,但在违规方法中没有引用
  • JClass是一个java类的表示,它是一个自定义结构
4

1 回答 1

1

通过更改您的配置以包含文档中提供的特定于区域的配置,并将区域传递给您的包装器,应该可以解决问题。

看起来您为所有包装器使用相同的缓存区域,因此在包装器中引用相同的“底层缓存”。你能把你所有的包装都改成mCache = JCS.getInstance("default");类似的东西吗?mCache = JCS.getInstance("uniqueNameForWrapper");

于 2015-06-09T00:22:30.047 回答