예제 #1
0
        public void StoreCacheItem(string key, ClientCacheItem value)
        {
            ServerCacheItem cacheItemWrapped;
            if (!_localCache.TryGetValue(key, out cacheItemWrapped))
                cacheItemWrapped = new ServerCacheItem() { ItemState = CacheItemState.None };

            cacheItemWrapped.Hash = Hasher.GetHash(key);
            cacheItemWrapped.Value = value;

            Func<string, ServerCacheItem, ServerCacheItem> updateValueFactory = ((x, y) => (cacheItemWrapped));
            _localCache.AddOrUpdate(key, cacheItemWrapped, updateValueFactory);

            //has the item been moved to another node?
            if (cacheItemWrapped.ItemState == CacheItemState.Moved)
            {
                Debug.Assert(cacheItemWrapped.RelocatedTo != null);
                Node self = new Node() { EndPoint = TcpHelper.SelfIPAddress, NodeState = NodeState.Active };

                //prepare the connection
                string endPoint = string.Format("net.tcp://{0}:{1}/HoCCacheService", cacheItemWrapped.RelocatedTo.EndPoint.ToString(), cacheItemWrapped.RelocatedTo.ServicePort);
                CacheServiceReference.CacheServiceClient nodeService = new CacheServiceReference.CacheServiceClient(new NetTcpBinding(SecurityMode.None), new EndpointAddress(endPoint));

                //update to the target node.
                nodeService.StoreCacheItem(key, value);
            }
        }
예제 #2
0
파일: Cache.cs 프로젝트: nittikkin/hoc
        public object this[string key]
        {
            get
            {
                try
                {
                    ClientCacheItem cacheItem = GetCacheServiceClient(key).RetrieveCacheItem(key);
                    if (cacheItem == null)
                        return null;

                    byte[] dataArray = cacheItem.Value;
                    if (_useCompression)
                    {
                        dataArray = DeCompress(cacheItem.Value);
                    }
                    return DeSerialize(dataArray, cacheItem.TypeAsString);
                }
                catch
                {
                    return null; //return null in case of any connection error
                }
            }

            set
            {
                MemoryStream dataStream = Serialize(value);
                byte[] objectAsBytes = dataStream.ToArray();
                if (_useCompression)
                {
                    objectAsBytes = Compress(dataStream);

                }
                ClientCacheItem cacheItem = new ClientCacheItem()
                {
                    TypeAsString = value.GetType().AssemblyQualifiedName,
                    Value = objectAsBytes
                };
                GetCacheServiceClient(key).StoreCacheItem(key, cacheItem);
            }
        }