Exemplo n.º 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);
            }
        }