Exemplo n.º 1
0
        public OperationResult CheckAndSet(string key, uint flags, long expirationTimeInSeconds, ulong casUnique, object dataBlock)
        {
            if (string.IsNullOrEmpty(key) || dataBlock == null)
            {
                ThrowInvalidArgumentsException();
            }

            OperationResult returnObject = new OperationResult();

            try
            {
                CacheItem getCacheItem = _cache.GetCacheItem(key);
                if (getCacheItem == null)
                {
                    returnObject = CreateReturnObject(Result.ITEM_NOT_FOUND, null);
                }
                else
                {
                    MemcachedItem memCacheItem = (MemcachedItem)getCacheItem.Value;
                    if (memCacheItem.InternalVersion == casUnique)
                    {
                        returnObject = InsertItemSuccessfully(key, flags, expirationTimeInSeconds, dataBlock);
                    }
                    else
                    {
                        returnObject = CreateReturnObject(Result.ITEM_MODIFIED, null);
                    }
                }
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }
            return(returnObject);
        }
Exemplo n.º 2
0
        public OperationResult Delete(string key, ulong casUnique)
        {
            if (string.IsNullOrEmpty(key))
            {
                ThrowInvalidArgumentsException();
            }

            OperationResult returnObject = new OperationResult();

            try
            {
                if (casUnique == 0)
                {
                    Object obj = _cache.Remove(key);
                    if (obj == null)
                    {
                        returnObject = CreateReturnObject(Result.ITEM_NOT_FOUND, null);
                    }
                    else
                    {
                        returnObject = CreateReturnObject(Result.SUCCESS, null);
                    }
                }
                else
                {
                    CacheItem item = _cache.GetCacheItem(key);
                    if (item == null)
                    {
                        returnObject = CreateReturnObject(Result.ITEM_NOT_FOUND, null);
                    }
                    else
                    {
                        MemcachedItem memCacheItem = (MemcachedItem)item.Value;
                        if (memCacheItem.InternalVersion != casUnique)
                        {
                            returnObject = CreateReturnObject(Result.ITEM_MODIFIED, null);
                        }
                        else
                        {
                            _cache.Delete(key);
                            returnObject = CreateReturnObject(Result.SUCCESS, null);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }

            return(returnObject);
        }
Exemplo n.º 3
0
        private GetOpResult CreateGetObject(string key, CacheItem cacheItem)
        {
            GetOpResult     getObject       = new GetOpResult();
            MemcachedItem   memCacheItem    = (MemcachedItem)cacheItem.Value;
            ObjectArrayData objectArrayData = GetObjectArrayData(memCacheItem.Data);

            getObject.Key          = key;
            getObject.Flag         = objectArrayData.flags;
            getObject.Value        = objectArrayData.dataBytes;
            getObject.Version      = memCacheItem.InternalVersion;
            getObject.ReturnResult = Result.SUCCESS;
            return(getObject);
        }
Exemplo n.º 4
0
        //whenever the item is updated, the version is incremented by 1
        private CacheItem CreateCacheItem(uint flags, object dataBlock, long expirationTimeInSeconds, ulong version)
        {
            MemcachedItem memCacheItem = new MemcachedItem();

            memCacheItem.Data            = CreateObjectArray(flags, dataBlock);
            memCacheItem.InternalVersion = version;
            CacheItem cacheItem = new CacheItem(memCacheItem);

            ;
            if (expirationTimeInSeconds != 0)
            {
                cacheItem.AbsoluteExpiration = CreateExpirationDate(expirationTimeInSeconds);
            }
            return(cacheItem);
        }
Exemplo n.º 5
0
        private MutateOpResult Mutate(string key, ulong value, object initialValue, long expirationTimeInSeconds, ulong casUnique, UpdateType updateType)
        {
            if (string.IsNullOrEmpty(key) || (initialValue != null && IsUnsignedNumeric(initialValue) == false))
            {
                ThrowInvalidArgumentsException();
            }

            MutateOpResult returnObject = new MutateOpResult();

            try
            {
                CacheItem getObject = _cache.GetCacheItem(key);
                if (getObject == null)
                {
                    if (initialValue == null || expirationTimeInSeconds == uint.MaxValue)
                    {
                        returnObject.ReturnResult = Result.ITEM_NOT_FOUND;
                        returnObject.Value        = null;
                    }
                    else
                    {
                        OperationResult opResult = InsertItemSuccessfully(key, 10, expirationTimeInSeconds,
                                                                          BitConverter.GetBytes(Convert.ToUInt32(initialValue)));
                        returnObject.Value        = opResult.Value;
                        returnObject.ReturnResult = opResult.ReturnResult;
                        returnObject.MutateResult = Convert.ToUInt64(initialValue);
                    }
                }
                else
                {
                    MemcachedItem memCacheItem = (MemcachedItem)getObject.Value;
                    if ((casUnique > 0 && memCacheItem.InternalVersion == casUnique) || casUnique == 0)
                    {
                        returnObject = UpdateIfNumeric(key, getObject, value, updateType);
                    }
                    else
                    {
                        returnObject.ReturnResult = Result.ITEM_MODIFIED;
                        returnObject.Value        = null;
                    }
                }
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }
            return(returnObject);
        }
Exemplo n.º 6
0
        private OperationResult JoinObjects(string key, CacheItem cacheItem, object objectToJoin, UpdateType updateType)
        {
            OperationResult returnObject    = new OperationResult();
            ObjectArrayData objectDataArray = GetObjectArrayData(((MemcachedItem)cacheItem.Value).Data);

            byte[] originalByteObject = objectDataArray.dataBytes;
            byte[] byteObjectToJoin   = (byte[])objectToJoin;

            byte[] joinedObject = new byte[originalByteObject.Length + byteObjectToJoin.Length];

            if (updateType == UpdateType.Append)
            {
                System.Buffer.BlockCopy(originalByteObject, 0, joinedObject, 0, originalByteObject.Length);
                System.Buffer.BlockCopy(byteObjectToJoin, 0, joinedObject, originalByteObject.Length, byteObjectToJoin.Length);
            }
            else
            {
                System.Buffer.BlockCopy(byteObjectToJoin, 0, joinedObject, 0, byteObjectToJoin.Length);
                System.Buffer.BlockCopy(originalByteObject, 0, joinedObject, byteObjectToJoin.Length, originalByteObject.Length);
            }

            try
            {
                MemcachedItem memCacheItem = new MemcachedItem();
                memCacheItem.Data = CreateObjectArray(objectDataArray.flags, joinedObject);

                ulong getVersion = GetLatestVersion();

                memCacheItem.InternalVersion = getVersion;

                cacheItem.Value = memCacheItem;

                _cache.Insert(key, cacheItem);

                returnObject = CreateReturnObject(Result.SUCCESS, getVersion);
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }

            return(returnObject);
        }
Exemplo n.º 7
0
        private OperationResult Concat(string key, object dataToPrepend, ulong casUnique, UpdateType updateType)
        {
            if (string.IsNullOrEmpty(key) || dataToPrepend == null)
            {
                ThrowInvalidArgumentsException();
            }

            OperationResult returnObject = new OperationResult();

            try
            {
                CacheItem getObject = _cache.GetCacheItem(key);
                if (getObject == null)
                {
                    returnObject = CreateReturnObject(Result.ITEM_NOT_FOUND, null);
                }
                else if (getObject.Value == null)
                {
                    returnObject = CreateReturnObject(Result.ITEM_NOT_FOUND, null);
                }
                else
                {
                    MemcachedItem memCacheItem = (MemcachedItem)getObject.Value;
                    if ((casUnique > 0 && memCacheItem.InternalVersion == casUnique) || casUnique == 0)
                    {
                        returnObject = JoinObjects(key, getObject, dataToPrepend, updateType);
                    }
                    else
                    {
                        returnObject = CreateReturnObject(Result.ITEM_MODIFIED, null);
                    }
                }
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }
            return(returnObject);
        }
Exemplo n.º 8
0
        private MutateOpResult UpdateIfNumeric(string key, CacheItem cacheItem, ulong value, UpdateType updateType)
        {
            MutateOpResult returnObject  = new MutateOpResult();
            MemcachedItem  memCachedItem = (MemcachedItem)cacheItem.Value;

            if (memCachedItem != null)
            {
                ObjectArrayData objectDataArray = GetObjectArrayData(memCachedItem.Data);

                string tempObjectString = "";
                try
                {
                    tempObjectString = Encoding.ASCII.GetString(objectDataArray.dataBytes);
                }
                catch (Exception e)
                {
                    ThrowCacheRuntimeException(e);
                }

                if (IsUnsignedNumeric(tempObjectString))
                {
                    ulong originalValue = Convert.ToUInt64(tempObjectString);
                    ulong finalValue;

                    if (updateType == UpdateType.Increment)
                    {
                        finalValue = originalValue + value;
                    }
                    else
                    {
                        if (value > originalValue)
                        {
                            finalValue = 0;
                        }
                        else
                        {
                            finalValue = originalValue - value;
                        }
                    }

                    try
                    {
                        MemcachedItem memCacheItem = new MemcachedItem();
                        memCacheItem.Data = CreateObjectArray(objectDataArray.flags, Encoding.ASCII.GetBytes(finalValue + ""));


                        ulong getVersion = GetLatestVersion();

                        memCacheItem.InternalVersion = getVersion;

                        cacheItem.Value = memCacheItem;

                        _cache.Insert(key, cacheItem);

                        returnObject.ReturnResult = Result.SUCCESS;
                        returnObject.Value        = getVersion;
                        returnObject.MutateResult = finalValue;
                    }
                    catch (Exception e)
                    {
                        ThrowCacheRuntimeException(e);
                    }
                }
                else
                {
                    returnObject.ReturnResult = Result.ITEM_TYPE_MISMATCHED;
                    returnObject.Value        = null;
                    returnObject.MutateResult = 0;
                }
            }
            return(returnObject);
        }
Exemplo n.º 9
0
        private MutateOpResult UpdateIfNumeric(string key, CacheItem cacheItem, ulong value, UpdateType updateType)
        {
            MutateOpResult returnObject = new MutateOpResult();
            MemcachedItem memCachedItem = (MemcachedItem)cacheItem.Value;
            if (memCachedItem != null)
            {
                ObjectArrayData objectDataArray = GetObjectArrayData(memCachedItem.Data);

                string tempObjectString = "";
                try
                {
                    tempObjectString = Encoding.ASCII.GetString(objectDataArray.dataBytes);
                }
                catch (Exception e)
                {
                    ThrowCacheRuntimeException(e);
                }

                if (IsUnsignedNumeric(tempObjectString))
                {
                    ulong originalValue = Convert.ToUInt64(tempObjectString);
                    ulong finalValue;

                    if (updateType == UpdateType.Increment)
                    {
                        finalValue = originalValue + value;
                    }
                    else
                    {
                        if (value > originalValue)
                            finalValue = 0;
                        else
                            finalValue = originalValue - value;
                    }

                    try
                    {

                        MemcachedItem memCacheItem = new MemcachedItem();
                        memCacheItem.Data = CreateObjectArray(objectDataArray.flags, Encoding.ASCII.GetBytes(finalValue + ""));


                        ulong getVersion = GetLatestVersion();

                        memCacheItem.InternalVersion = getVersion;

                        cacheItem.Value = memCacheItem;

                        _cache.Insert(key, cacheItem);

                        returnObject.ReturnResult = Result.SUCCESS;
                        returnObject.Value = getVersion;
                        returnObject.MutateResult = finalValue;
                    }
                    catch (Exception e)
                    {
                        ThrowCacheRuntimeException(e);
                    }
                }
                else
                {
                    returnObject.ReturnResult = Result.ITEM_TYPE_MISMATCHED;
                    returnObject.Value = null;
                    returnObject.MutateResult = 0;
                }
            }
            return returnObject;
        }
Exemplo n.º 10
0
        private OperationResult JoinObjects(string key, CacheItem cacheItem, object objectToJoin, UpdateType updateType)
        {
            OperationResult returnObject = new OperationResult();
            ObjectArrayData objectDataArray = GetObjectArrayData(((MemcachedItem)cacheItem.Value).Data);
            byte[] originalByteObject = objectDataArray.dataBytes;
            byte[] byteObjectToJoin = (byte[])objectToJoin;

            byte[] joinedObject = new byte[originalByteObject.Length + byteObjectToJoin.Length];

            if (updateType == UpdateType.Append)
            {
                System.Buffer.BlockCopy(originalByteObject, 0, joinedObject, 0, originalByteObject.Length);
                System.Buffer.BlockCopy(byteObjectToJoin, 0, joinedObject, originalByteObject.Length, byteObjectToJoin.Length);
            }
            else
            {
                System.Buffer.BlockCopy(byteObjectToJoin, 0, joinedObject, 0, byteObjectToJoin.Length);
                System.Buffer.BlockCopy(originalByteObject, 0, joinedObject, byteObjectToJoin.Length, originalByteObject.Length);
            }

            try
            {
                
                MemcachedItem memCacheItem = new MemcachedItem();
                memCacheItem.Data = CreateObjectArray(objectDataArray.flags, joinedObject);
                
                ulong getVersion = GetLatestVersion();
               
                memCacheItem.InternalVersion = getVersion;
                
                cacheItem.Value = memCacheItem;
                
                _cache.Insert(key, cacheItem);
                
                returnObject = CreateReturnObject(Result.SUCCESS, getVersion);
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }

            return returnObject;
        }
Exemplo n.º 11
0
     //whenever the item is updated, the version is incremented by 1
     private CacheItem CreateCacheItem(uint flags, object dataBlock, long expirationTimeInSeconds, ulong version)
     {
         MemcachedItem memCacheItem = new MemcachedItem();
         memCacheItem.Data = CreateObjectArray(flags, dataBlock);
         memCacheItem.InternalVersion = version;
         CacheItem cacheItem = new CacheItem(memCacheItem);
 ;
         if (expirationTimeInSeconds != 0)
             cacheItem.AbsoluteExpiration = CreateExpirationDate(expirationTimeInSeconds);
         return cacheItem;
     }