/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * Kind, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ using PortCMIS.Utils; using System; using System.Collections.Generic; namespace PortCMIS.Client { /// /// Client object cache interface. /// public interface ICache { /// /// Initializes the cache. /// /// the session /// cache parameters void Initialize(ISession session, IDictionary parameters); /// /// Returns whether the cache contains an object with given object ID and cache key. /// /// the object ID /// the cache key /// true if the object is in the cache, false otherwise bool ContainsId(string objectId, string cacheKey); /// /// Returns whether the cache contains an object with given path and cache key. /// /// the path /// the cache key /// true if the object is in the cache, false otherwise bool ContainsPath(string path, string cacheKey); /// /// Puts an object into the cache. /// /// the object /// the cache key void Put(ICmisObject cmisObject, string cacheKey); /// /// Puts an object with a path into the cache. /// /// the path /// the object /// the cache key void PutPath(string path, ICmisObject cmisObject, string cacheKey); /// /// Gets an object by ID. /// /// the object ID /// the cache key /// the object or null if the object is not in the cache ICmisObject GetById(string objectId, string cacheKey); /// /// Gets an object by path. /// /// the path /// the cache key /// the object or null if the object is not in the cache ICmisObject GetByPath(string path, string cacheKey); /// /// Gets the object ID by path. /// /// the path /// the object ID string GetObjectIdByPath(string path); /// /// Removes an object from the cache. /// /// the object ID void Remove(string objectId); /// /// Removes a path from the cache. /// /// the path void RemovePath(string path); /// /// Clears the cache. /// void Clear(); /// /// The number of objects in the cache. /// int CacheSize { get; } } /// /// Cache implementation that doesn't cache anything. /// public class NoCache : ICache { /// public void Initialize(ISession session, IDictionary parameters) { } /// public bool ContainsId(string objectId, string cacheKey) { return false; } /// public bool ContainsPath(string path, string cacheKey) { return false; } /// public void Put(ICmisObject cmisObject, string cacheKey) { } /// public void PutPath(string path, ICmisObject cmisObject, string cacheKey) { } /// public ICmisObject GetById(string objectId, string cacheKey) { return null; } /// public ICmisObject GetByPath(string path, string cacheKey) { return null; } /// public string GetObjectIdByPath(string path) { return null; } /// public void Remove(string objectId) { } /// public void RemovePath(string path) { } /// public void Clear() { } /// public int CacheSize { get { return 0; } } } /// /// Default object cache implementation. /// public class CmisObjectCache : ICache { private int cacheSize; private int cacheTtl; private int pathToIdSize; private int pathToIdTtl; private LRUCache> objectCache; private LRUCache pathToIdCache; private object cacheLock = new object(); /// /// Constructor. /// public CmisObjectCache() { } /// public void Initialize(ISession session, IDictionary parameters) { lock (cacheLock) { // cache size cacheSize = 1000; try { string cacheSizeStr; if (parameters.TryGetValue(SessionParameter.CacheSizeObjects, out cacheSizeStr)) { cacheSize = Int32.Parse(cacheSizeStr); if (cacheSize < 0) { cacheSize = 0; } } } catch (Exception) { } // cache time-to-live cacheTtl = 2 * 60 * 60 * 1000; try { string cacheTtlStr; if (parameters.TryGetValue(SessionParameter.CacheTTLObjects, out cacheTtlStr)) { cacheTtl = Int32.Parse(cacheTtlStr); if (cacheTtl < 0) { cacheTtl = 2 * 60 * 60 * 1000; } } } catch (Exception) { } // path-to-id size pathToIdSize = 1000; try { string pathToIdSizeStr; if (parameters.TryGetValue(SessionParameter.CacheSizePathToId, out pathToIdSizeStr)) { pathToIdSize = Int32.Parse(pathToIdSizeStr); if (pathToIdSize < 0) { pathToIdSize = 0; } } } catch (Exception) { } // path-to-id time-to-live pathToIdTtl = 30 * 60 * 1000; try { string pathToIdTtlStr; if (parameters.TryGetValue(SessionParameter.CacheTTLPathToId, out pathToIdTtlStr)) { pathToIdTtl = Int32.Parse(pathToIdTtlStr); if (pathToIdTtl < 0) { pathToIdTtl = 30 * 60 * 1000; } } } catch (Exception) { } InitializeInternals(); } } private void InitializeInternals() { lock (cacheLock) { objectCache = new LRUCache>(cacheSize, TimeSpan.FromMilliseconds(cacheTtl)); pathToIdCache = new LRUCache(pathToIdSize, TimeSpan.FromMilliseconds(pathToIdTtl)); } } /// public void Clear() { InitializeInternals(); } /// public bool ContainsId(string objectId, string cacheKey) { lock (cacheLock) { return objectCache.Get(objectId) != null; } } /// public bool ContainsPath(string path, string cacheKey) { lock (cacheLock) { return pathToIdCache.Get(path) != null; } } /// public ICmisObject GetById(string objectId, string cacheKey) { lock (cacheLock) { IDictionary cacheKeyDict = objectCache.Get(objectId); if (cacheKeyDict == null) { return null; } ICmisObject cmisObject; if (cacheKeyDict.TryGetValue(cacheKey, out cmisObject)) { return cmisObject; } return null; } } /// public ICmisObject GetByPath(string path, string cacheKey) { lock (cacheLock) { string id = pathToIdCache.Get(path); if (id == null) { return null; } return GetById(id, cacheKey); } } /// public void Put(ICmisObject cmisObject, string cacheKey) { // no object, no id, no cache key - no cache if (cmisObject == null || cmisObject.Id == null || cacheKey == null) { return; } lock (cacheLock) { IDictionary cacheKeyDict = objectCache.Get(cmisObject.Id); if (cacheKeyDict == null) { cacheKeyDict = new Dictionary(); objectCache.Add(cmisObject.Id, cacheKeyDict); } cacheKeyDict[cacheKey] = cmisObject; // folders may have a path, use it! string path = cmisObject.GetPropertyValue(PropertyIds.Path) as string; if (path != null) { pathToIdCache.Add(path, cmisObject.Id); } } } /// public void PutPath(string path, ICmisObject cmisObject, string cacheKey) { // no path, no object, no id, no cache key - no cache if (path == null || cmisObject == null || cmisObject.Id == null || cacheKey == null) { return; } lock (cacheLock) { Put(cmisObject, cacheKey); pathToIdCache.Add(path, cmisObject.Id); } } /// public string GetObjectIdByPath(string path) { lock (cacheLock) { return pathToIdCache.Get(path); } } /// public void Remove(string objectId) { if (objectId == null) { return; } lock (cacheLock) { objectCache.Remove(objectId); } } /// public void RemovePath(string path) { if (path == null) { return; } lock (cacheLock) { pathToIdCache.Remove(path); } } /// public int CacheSize { get { return cacheSize; } } } }