Exemplo n.º 1
0
        /// <summary>
        /// Removes all serialized objects associated with the given tag names and optionally with keys matching the given pattern.
        /// WARNING: THIS IS A VERY EXPENSIVE OPERATION FOR LARGE TAG CACHES. USE WITH CAUTION.
        /// </summary>
        /// <param name="tagNames">The tag names.</param>
        /// <param name="pattern">The search pattern (RegEx). Optional. If not specified, the default of "*" is used to indicate match all.</param>
        public void RemoveTagged(IEnumerable<string> tagNames, string pattern = "*")
        {
            // Sanitize
            if (tagNames == null)
            {
                throw new ArgumentNullException("tagNames");
            }
            if (!tagNames.Any())
            {
                throw new ArgumentException("must have at least one element", "tagNames");
            }
            if (string.IsNullOrWhiteSpace(pattern))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "pattern");
            }

            byte[] command;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("del-tag");
                memoryStream.WriteSpace();
                memoryStream.Write(pattern);
                foreach (var tagName in tagNames)
                {
                    memoryStream.WriteSpace();
                    memoryStream.Write(tagName);
                }
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.RepeatingCacheKeys);
            // Send
            _client.Send(command);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets all serialized objects associated with the given tag name.
        /// </summary>
        /// <param name="tagNames">The tag names.</param>
        /// <returns>A list of the serialized objects.</returns>
        public List<byte[]> GetTagged(IEnumerable<string> tagNames)
        {
            // Sanitize
            if (tagNames == null)
            {
                throw new ArgumentNullException("tagNames");
            }
            if (!tagNames.Any())
            {
                throw new ArgumentException("must have at least one element", "tagNames");
            }

            byte[] command = null;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("get-tag");
                foreach (var tagName in tagNames)
                {
                    memoryStream.WriteSpace();
                    memoryStream.Write(tagName);
                }
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.RepeatingCacheKeys);
            // Send and receive
            command = _client.SendReceive(command);
            // Parse string
            var commandResult = DacheProtocolHelper.CommunicationEncoding.GetString(command);

            // Verify that we got something
            if (commandResult == null)
            {
                return null;
            }

            // Parse command from bytes
            var commandResultParts = commandResult.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            return ParseCacheObjects(commandResultParts);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Removes the serialized objects at the given cache keys from the cache.
        /// </summary>
        /// <param name="cacheKeys">The cache keys.</param>
        public void Remove(IEnumerable<string> cacheKeys)
        {
            // Sanitize
            if (cacheKeys == null)
            {
                throw new ArgumentNullException("cacheKeys");
            }
            if (!cacheKeys.Any())
            {
                throw new ArgumentException("must have at least one element", "cacheKeys");
            }

            byte[] command = null;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("del");
                foreach (var cacheKey in cacheKeys)
                {
                    memoryStream.WriteSpace();
                    memoryStream.Write(cacheKey);
                }
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.RepeatingCacheKeys);
            // Send
            _client.Send(command);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets all cache keys associated with the given tag names and optionally matching the given pattern.
        /// WARNING: THIS IS A VERY EXPENSIVE OPERATION FOR LARGE TAG CACHES. USE WITH CAUTION.
        /// </summary>
        /// <param name="tagNames">The tag names.</param>
        /// <param name="pattern">The search pattern (RegEx). Optional. If not specified, the default of "*" is used to indicate match all.</param>
        /// <returns>The list of cache keys matching the provided pattern.</returns>
        public List<byte[]> GetCacheKeysTagged(IEnumerable<string> tagNames, string pattern = "*")
        {
            if (tagNames == null)
            {
                throw new ArgumentNullException("tagNames");
            }
            if (!tagNames.Any())
            {
                throw new ArgumentException("must have at least one element", "tagNames");
            }
            if (string.IsNullOrWhiteSpace(pattern))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "pattern");
            }

            byte[] command;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("keys-tag");
                memoryStream.WriteSpace();
                memoryStream.Write(pattern);
                foreach (var tagName in tagNames)
                {
                    memoryStream.WriteSpace();
                    memoryStream.Write(tagName);
                }
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.Literal);
            // Send and receive
            var rawResult = _client.SendReceive(command);

            // Verify that we got something
            if (rawResult == null)
            {
                return null;
            }

            // Parse string
            var decodedResult = DacheProtocolHelper.CommunicationEncoding.GetString(rawResult);
            if (string.IsNullOrWhiteSpace(decodedResult))
            {
                return null;
            }

            // Parse command from bytes
            var commandResultParts = decodedResult.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            return ParseCacheObjects(commandResultParts);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets all cache keys, optionally matching the provided pattern.
        /// WARNING: THIS IS A VERY EXPENSIVE OPERATION FOR LARGE CACHES. USE WITH CAUTION.
        /// </summary>
        /// <param name="pattern">The search pattern (RegEx). Optional. If not specified, the default of "*" is used to indicate match all.</param>
        /// <returns>The list of cache keys matching the provided pattern</returns>
        public List<byte[]> GetCacheKeys(string pattern = "*")
        {
            // Sanitize
            if (string.IsNullOrWhiteSpace(pattern))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "pattern");
            }

            byte[] command;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("keys {0}", pattern);
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.Literal);
            // Send and receive
            var rawResult = _client.SendReceive(command);

            // Verify that we got something
            if (rawResult == null)
            {
                return null;
            }

            // Parse string
            var decodedResult = DacheProtocolHelper.CommunicationEncoding.GetString(rawResult);
            if (string.IsNullOrWhiteSpace(decodedResult))
            {
                return null;
            }

            // Parse command from bytes
            var commandResultParts = decodedResult.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            return ParseCacheObjects(commandResultParts);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Clears the cache.
        /// </summary>
        public void Clear()
        {
            byte[] command;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("clear");
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.Literal);
            // Send
            _client.Send(command);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds or updates the interned serialized objects in the cache at the given cache keys.
        /// NOTE: interned objects use significantly less memory when placed in the cache multiple times however cannot expire or be evicted. 
        /// You must remove them manually when appropriate or else you may face a memory leak.
        /// </summary>
        /// <param name="cacheKeysAndSerializedObjects">The cache keys and associated serialized objects.</param>
        /// <param name="tagName">The tag name.</param>
        public void AddOrUpdateTaggedInterned(IEnumerable<KeyValuePair<string, byte[]>> cacheKeysAndSerializedObjects, string tagName)
        {
            // Sanitize
            if (cacheKeysAndSerializedObjects == null)
            {
                throw new ArgumentNullException("cacheKeysAndSerializedObjects");
            }
            if (!cacheKeysAndSerializedObjects.Any())
            {
                throw new ArgumentException("must have at least one element", "cacheKeysAndSerializedObjects");
            }
            if (string.IsNullOrWhiteSpace(tagName))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "tagName");
            }

            byte[] command = null;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("set-tag-intern {0}", tagName);
                foreach (var cacheKeyAndSerializedObjectKvp in cacheKeysAndSerializedObjects)
                {
                    memoryStream.WriteSpace();
                    memoryStream.Write(cacheKeyAndSerializedObjectKvp.Key);
                    memoryStream.WriteSpace();
                    memoryStream.WriteBase64(cacheKeyAndSerializedObjectKvp.Value);
                }
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.RepeatingCacheKeysAndObjects);
            // Send
            _client.Send(command);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds or updates the serialized objects in the cache at the given cache keys.
        /// </summary>
        /// <param name="cacheKeysAndSerializedObjects">The cache keys and associated serialized objects.</param>
        /// <param name="slidingExpiration">The sliding expiration.</param>
        public void AddOrUpdate(IEnumerable<KeyValuePair<string, byte[]>> cacheKeysAndSerializedObjects, TimeSpan slidingExpiration)
        {
            // Sanitize
            if (cacheKeysAndSerializedObjects == null)
            {
                throw new ArgumentNullException("cacheKeysAndSerializedObjects");
            }
            if (!cacheKeysAndSerializedObjects.Any())
            {
                throw new ArgumentException("must have at least one element", "cacheKeysAndSerializedObjects");
            }

            byte[] command = null;
            using (var memoryStream = new MemoryStream())
            {
                memoryStream.WriteControlBytePlaceHolder();
                memoryStream.Write("set {0}", (int)slidingExpiration.TotalSeconds);
                foreach (var cacheKeyAndSerializedObjectKvp in cacheKeysAndSerializedObjects)
                {
                    memoryStream.WriteSpace();
                    memoryStream.Write(cacheKeyAndSerializedObjectKvp.Key);
                    memoryStream.WriteSpace();
                    memoryStream.WriteBase64(cacheKeyAndSerializedObjectKvp.Value);
                }
                command = memoryStream.ToArray();
            }

            // Set control byte
            command.SetControlByte(DacheProtocolHelper.MessageType.RepeatingCacheKeysAndObjects);
            // Send
            _client.Send(command);
        }