Esempio n. 1
0
        private void ProcessCommand(string command, byte[] data, int position)
        {
            // Sanitize
            if (string.IsNullOrWhiteSpace(command) || data == null || data.Length == 0 || position <= -1)
            {
                return;
            }

            string[] commandParts = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (commandParts.Length == 0)
            {
                return;
            }

            // Determine command
            if (string.Equals(commandParts[0], "expire", StringComparison.OrdinalIgnoreCase))
            {
                // Invalidate local cache keys
                while (position < data.Length)
                {
                    var result = DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(data, ref position));
                    // Fire the cache item expired event
                    var cacheItemExpired = CacheItemExpired;
                    if (cacheItemExpired != null)
                    {
                        cacheItemExpired(this, new CacheItemExpiredArgs(result));
                    }
                }
            }
        }
Esempio n. 2
0
        private void ReceiveMessage(object sender, MessageReceivedArgs e)
        {
            var command = e.ReceivedMessage.Message;

            if (command == null || command.Length == 0)
            {
                return;
            }

            // Get the command string
            int position      = 0;
            var commandString = DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(command, ref position));

            // Right now this is only used for invalidating cache keys, so there will never be a reply
            ProcessCommand(commandString, command, position);
        }
Esempio n. 3
0
        private List <byte[]> SendGet(IEnumerable <string> cacheKeysOrTagNames, bool isTagNames = false, string pattern = "*")
        {
            byte[] command = null;

            var sb = new StringBuilder();

            sb.Append("get");
            sb.Append(" ").Append(pattern);

            if (isTagNames)
            {
                sb.Append(" -t");
            }

            using (var memoryStream = new MemoryStream())
            {
                memoryStream.Write(sb.ToString());

                foreach (var cacheKeyOrTagName in cacheKeysOrTagNames)
                {
                    memoryStream.Write(cacheKeyOrTagName);
                }
                command = memoryStream.ToArray();
            }

            // Send and receive
            command = _client.SendReceive(command);

            // Verify that we got something
            if (command == null || (command.Length == 1 && command[0] == 0))
            {
                return(null);
            }

            // Get command result
            var commandResultParts = new List <byte[]>();
            int position           = 0;

            while (position < command.Length)
            {
                commandResultParts.Add(DacheProtocolHelper.Extract(command, ref position));
            }

            return(commandResultParts);
        }
Esempio n. 4
0
        private void ReceiveMessage(object sender, MessageReceivedArgs e)
        {
            var command = e.ReceivedMessage.Message;

            if (command == null || command.Length == 0)
            {
                return;
            }

            // Get the command string
            int position      = 0;
            var commandString = DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(command, ref position));

            var commandResult = ProcessCommand(commandString, command, position);

            if (commandResult != null)
            {
                // Send the result if there is one
                _server.Reply(commandResult, e.ReceivedMessage);
            }
        }
Esempio n. 5
0
        private IEnumerable <KeyValuePair <string, byte[]> > ParseCacheKeysAndObjects(byte[] data, int position)
        {
            var    cacheKeysAndObjects = new List <KeyValuePair <string, byte[]> >();
            int    i        = 0;
            string cacheKey = null;

            while (position < data.Length)
            {
                var result = DacheProtocolHelper.Extract(data, ref position);
                if (i % 2 == 0)
                {
                    cacheKey = DacheProtocolHelper.CommunicationEncoding.GetString(result);
                }
                else
                {
                    cacheKeysAndObjects.Add(new KeyValuePair <string, byte[]>(cacheKey, result));
                }

                unchecked { i++; }
            }

            return(cacheKeysAndObjects);
        }
Esempio n. 6
0
        private byte[] ProcessCommand(string command, byte[] data, int position)
        {
            // Sanitize
            if (string.IsNullOrWhiteSpace(command) || data == null || data.Length == 0 || position <= -1)
            {
                return(null);
            }

            string[] commandParts = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (commandParts.Length == 0)
            {
                return(null);
            }

            byte[] commandResult = null;

            // Determine command
            switch (commandParts[0].ToLowerInvariant())
            {
            case "clear":
            {
                Clear();
                break;
            }

            case "keys":
            {
                var           pattern = commandParts[1];
                List <string> results = null;

                // Check for tags
                if (commandParts.Length == 3 && string.Equals(commandParts[2], "-t", StringComparison.OrdinalIgnoreCase))
                {
                    List <string> tagNames = new List <string>();
                    while (position < data.Length)
                    {
                        tagNames.Add(DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(data, ref position)));
                    }

                    results = GetCacheKeysTagged(tagNames, pattern);
                }
                else
                {
                    results = GetCacheKeys(pattern);
                }

                commandResult = CreateCommandResult(results);
                break;
            }

            case "get":
            {
                List <byte[]> results = null;

                // Check for tags
                if (commandParts.Length == 3 && string.Equals(commandParts[2], "-t", StringComparison.OrdinalIgnoreCase))
                {
                    var pattern = commandParts[1];

                    List <string> tagNames = new List <string>();
                    while (position < data.Length)
                    {
                        tagNames.Add(DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(data, ref position)));
                    }

                    results = GetTagged(tagNames, pattern);
                }
                else
                {
                    List <string> cacheKeys = new List <string>();
                    while (position < data.Length)
                    {
                        cacheKeys.Add(DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(data, ref position)));
                    }

                    results = Get(cacheKeys);
                }

                commandResult = CreateCommandResult(results);
                break;
            }

            case "del":
            {
                // Check for tags
                if (commandParts.Length == 3 && string.Equals(commandParts[2], "-t", StringComparison.OrdinalIgnoreCase))
                {
                    var pattern = commandParts[1];

                    List <string> tagNames = new List <string>();
                    while (position < data.Length)
                    {
                        tagNames.Add(DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(data, ref position)));
                    }

                    RemoveTagged(tagNames, pattern);
                }
                else
                {
                    List <string> cacheKeys = new List <string>();
                    while (position < data.Length)
                    {
                        cacheKeys.Add(DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(data, ref position)));
                    }

                    Remove(cacheKeys);
                }

                break;
            }

            case "set":
            {
                bool           isInterned         = false;
                string         tagName            = null;
                DateTimeOffset?absoluteExpiration = null;
                TimeSpan?      slidingExpiration  = null;
                bool           notifyRemoved      = false;

                // Check for flags
                for (int i = 1; i < commandParts.Length; i++)
                {
                    switch (commandParts[i].ToLowerInvariant())
                    {
                    // Interned
                    case "-i":
                    {
                        isInterned = true;
                        break;
                    }

                    // Tag name
                    case "-t":
                    {
                        tagName = commandParts[i + 1];
                        i++;
                        break;
                    }

                    // Absolute expiration
                    case "-a":
                    {
                        absoluteExpiration = DateTimeOffset.ParseExact(commandParts[i + 1], DacheProtocolHelper.AbsoluteExpirationFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
                        i++;
                        break;
                    }

                    // Sliding expiration
                    case "-s":
                    {
                        slidingExpiration = TimeSpan.FromSeconds(int.Parse(commandParts[i + 1]));
                        i++;
                        break;
                    }

                    // Callback
                    case "-c":
                    {
                        notifyRemoved = true;
                        break;
                    }

                    // Something invalid or end of command
                    default:
                    {
                        i = commandParts.Length;
                        break;
                    }
                    }
                }

                IEnumerable <KeyValuePair <string, byte[]> > cacheKeysAndObjects = ParseCacheKeysAndObjects(data, position);

                // Decide what to do
                if (isInterned)
                {
                    AddOrUpdate(cacheKeysAndObjects, tagName: tagName, isInterned: true);
                }
                else
                {
                    if (absoluteExpiration.HasValue)
                    {
                        AddOrUpdate(cacheKeysAndObjects, tagName: tagName, absoluteExpiration: absoluteExpiration.Value, notifyRemoved: notifyRemoved);
                    }
                    else if (slidingExpiration.HasValue)
                    {
                        AddOrUpdate(cacheKeysAndObjects, tagName: tagName, slidingExpiration: slidingExpiration.Value, notifyRemoved: notifyRemoved);
                    }
                    else
                    {
                        AddOrUpdate(cacheKeysAndObjects, tagName: tagName, notifyRemoved: notifyRemoved);
                    }
                }
                break;
            }

            default:
            {
                // Invalid command
                break;
            }
            }

            // Return the result - may be no results if there was no valid message
            return(commandResult);
        }
Esempio n. 7
0
        private List <string> SendGetCacheKeys(IEnumerable <string> tagNames = null, string pattern = "*")
        {
            byte[] command;

            var sb = new StringBuilder();

            sb.Append("keys");

            if (pattern != null)
            {
                sb.Append(" ").Append(pattern);
            }

            if (tagNames != null)
            {
                sb.Append(" -t");
            }

            using (var memoryStream = new MemoryStream())
            {
                memoryStream.Write(sb.ToString());

                if (tagNames != null)
                {
                    foreach (var tagName in tagNames)
                    {
                        memoryStream.Write(tagName);
                    }
                }

                command = memoryStream.ToArray();
            }

            // Send and receive
            command = _client.SendReceive(command);

            // Verify that we got something
            if (command == null || (command.Length == 1 && command[0] == 0))
            {
                return(null);
            }

            // Get command result
            var commandResultParts = new List <string>();
            int position           = 0;

            while (position < command.Length)
            {
                commandResultParts.Add(DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(command, ref position)));
            }

            return(commandResultParts);
        }
Esempio n. 8
0
        private bool SendIsSet(string cacheKey)
        {
            byte[] command;

            var sb = new StringBuilder();

            sb.Append("isset");
            sb.Append(" ");
            sb.Append(cacheKey);

            using (var memoryStream = new MemoryStream())
            {
                memoryStream.Write(sb.ToString());

                command = memoryStream.ToArray();
            }

            // Send and receive
            command = _client.SendReceive(command);

            // Verify that we got something
            if (command == null || (command.Length == 1 && command[0] == 0))
            {
                return(false);
            }

            // Get command result
            var commandResultParts = new List <string>();
            int position           = 0;

            while (position < command.Length)
            {
                commandResultParts.Add(DacheProtocolHelper.CommunicationEncoding.GetString(DacheProtocolHelper.Extract(command, ref position)));
            }

            return(Convert.ToBoolean(commandResultParts[0]));
        }