Ejemplo n.º 1
0
        private ObjectArrayData GetObjectArrayData(object retrievedObject)
        {
            byte[] objectArray = (byte[])retrievedObject;
            byte[] dataArray   = new byte[objectArray.Length - 4];
            System.Buffer.BlockCopy(objectArray, 4, dataArray, 0, dataArray.Length);

            ObjectArrayData objectArrayData = new ObjectArrayData();

            objectArrayData.flags     = BitConverter.ToUInt32(objectArray, 0);
            objectArrayData.dataBytes = dataArray;
            return(objectArrayData);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        private ObjectArrayData GetObjectArrayData(object retrievedObject)
        {
            byte[] objectArray = (byte[])retrievedObject;
            byte[] dataArray = new byte[objectArray.Length - 4];
            System.Buffer.BlockCopy(objectArray, 4, dataArray, 0, dataArray.Length);

            ObjectArrayData objectArrayData = new ObjectArrayData();
            objectArrayData.flags = BitConverter.ToUInt32(objectArray, 0);
            objectArrayData.dataBytes = dataArray;
            return objectArrayData;

        }