private String getCacheKey(PDPRequestCollection reqCol) { StringBuilder key = new StringBuilder(); for (Iterator<PDPRequest> requests = reqCol.iterator(); requests.hasNext();) { key.append(getCacheKey(requests.next())); } return key.toString(); }
private PDPRequestCollection findNonCachedRequests(final PDPRequestCollection reqInCol, final Map<PDPRequest, PDPResponse> allRequestsLookup) { PDPRequestCollection reqcol = new PDPRequestCollection(); for (Iterator<PDPRequest> reqIt = reqInCol.iterator(); reqIt.hasNext();) { PDPRequest req = reqIt.next(); // here check for cache PDPResponse resp = lookUp(req); // if not in cache... if (resp == null) { reqcol.add(req); } // also add with null to ensure correct order! allRequestsLookup.put(req, resp); } return reqcol; }
/** * Responsible for storing new found responses in the cache. * * @param lastResponse * @param allRequestsLookup */ private void putInCache(final PDPResponseCollection responseCol, final Map<PDPRequest, PDPResponse> allRequestsLookup) { for (Iterator<PDPResponse> resIt = responseCol.iterator(); resIt.hasNext();) { PDPResponse resp = resIt.next(); // put in cache... store(resp.getRequest(), resp); allRequestsLookup.put(resp.getRequest(), resp); } }
String requestCollectionCacheKey = getCacheKey(reqCol); Lock lock; synchronized (this) { PDPRequestCollection openRequests = findNonCachedRequests(reqCol, allRequestsLookup); if (!openRequests.isEmpty()) { lastResponse = getDelegate().request(openRequests); putInCache(lastResponse, allRequestsLookup);
private PDPResponse lookUp(final PDPRequest req) { if (m_stopped || m_maxCacheTime < 0) { // cache disabled return null; } String key = getCacheKey(req); CacheEntry cacheEntry = null; synchronized (m_lock) { cacheEntry = m_decisionCache.get(key); } long now = System.currentTimeMillis(); PDPResponse resp = null; if (cacheEntry != null && cacheEntry.getInserted() > (now - m_maxCacheTime) && (resp = cacheEntry.getResponse()) != null) { if (LOG.isTraceEnabled()) { LOG.trace("cache hit for <" + req + "> --return--> <" + resp + ">"); } m_hitCount.incrementAndGet(); // return copy to ensure that the request object is the input param! return new PDPResponse(resp.getDecision(), req, resp.getObligations()); } m_missCount.incrementAndGet(); return null; }
private void store(final PDPRequest request, final PDPResponse resp) { if (m_stopped || m_maxCacheTime < 0) { return; } // store copy to ensure no memory leeks because of pdp responses/requests! CacheEntry entry = new CacheEntry(new PDPResponse(resp.getDecision(), new PDPRequest(new Target()), resp.getObligations())); String key = getCacheKey(request); synchronized (m_lock) { int size = m_decisionCache.size(); if (size > m_maxEntries) { // don't insert if limit is exceeded! return; } m_decisionCache.put(key, entry); } }