Beispiel #1
0
        /// <summary>
        /// Get the specify record
        /// </summary>
        public PersistanceItemInfo Get(string key)
        {
            DbCommand cmd = _conn.CreateCommand();

            cmd.CommandText =
                "select ItemKey,DataRecordPosition,ContentLength,LastModifyTime,Properties" +
                " from BlockInfo" +
                " where ItemKey=@ItemKey;";

            DbParameter para1 = cmd.CreateParameter();

            para1.ParameterName = "@ItemKey";
            para1.Value         = key;

            cmd.Parameters.Add(para1);

            using (DbDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    PersistanceItemInfo item = new PersistanceItemInfo();
                    item.Key = reader.GetString(0);
                    item.DataRecordPosition = reader.GetInt64(1);
                    item.ContentLength      = reader.GetInt32(2);
                    item.ItemTime           = reader.GetDateTime(3);
                    item.Properties         = reader.GetInt32(4);

                    return(item);
                }
                else
                {
                    throw new StoringException("The specify item not found");
                }
            }
        }
        /// <summary>
        /// Store or update item
        /// </summary>
        public void Store(CacheItem item)
        {
            if (!_enableStoring)
            {
                throw new InvalidOperationException("Does not support storing");
            }

            lock (_syncObject)
            {
                try
                {
                    PersistanceItemInfo info = _indexAccess.Get(item.Key);

                    //update old record
                    _dataAccess.Update(info.DataRecordPosition, item.Content, item.Content.Length);
                    _indexAccess.Update(
                        item.Key, item.Content.Length,
                        item.ItemTime, item.Properties);
                }
                catch (StoringException)
                {
                    //and new record
                    long position             = _dataAccess.Add(item.Content);
                    PersistanceItemInfo info2 = new PersistanceItemInfo()
                    {
                        Key                = item.Key,
                        ContentLength      = item.Content.Length,
                        DataRecordPosition = position,
                        ItemTime           = item.ItemTime,
                        Properties         = item.Properties
                    };
                    _indexAccess.Add(info2);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Add a new record
        /// </summary>
        public void Add(PersistanceItemInfo item)
        {
            DbCommand cmd = _conn.CreateCommand();

            cmd.CommandText = "insert into BlockInfo" +
                              " (ItemKey,DataRecordPosition,ContentLength,LastModifyTime,Properties)" +
                              " values (@ItemKey,@DataRecordPosition,@ContentLength,@LastModifyTime,@Properties);";

            DbParameter para1 = cmd.CreateParameter();
            DbParameter para2 = cmd.CreateParameter();
            DbParameter para3 = cmd.CreateParameter();
            DbParameter para4 = cmd.CreateParameter();
            DbParameter para5 = cmd.CreateParameter();

            para1.ParameterName = "@ItemKey";
            para1.Value         = item.Key;
            para2.ParameterName = "@DataRecordPosition";
            para2.Value         = item.DataRecordPosition;
            para3.ParameterName = "@ContentLength";
            para3.Value         = item.ContentLength;
            para4.ParameterName = "@LastModifyTime";
            para4.Value         = item.ItemTime;
            para5.ParameterName = "@Properties";
            para5.Value         = item.Properties;

            cmd.Parameters.Add(para1);
            cmd.Parameters.Add(para2);
            cmd.Parameters.Add(para3);
            cmd.Parameters.Add(para4);
            cmd.Parameters.Add(para5);

            cmd.ExecuteNonQuery();
        }
        /// <summary>
        /// Fetch cache infomation and content
        /// </summary>
        public CacheItem Fetch(string key)
        {
            //NOTE::
            // if the specify cache doesn't exist, this procedure will throw exception
            if (!_enableStoring)
            {
                throw new InvalidOperationException("Does not support storing");
            }

            lock (_syncObject)
            {
                PersistanceItemInfo info = _indexAccess.Get(key);
                byte[] data = new byte[info.ContentLength];
                _dataAccess.Get(info.DataRecordPosition, data, data.Length);

                CacheItem item = new CacheItem(
                    key, data, info.ItemTime, info.Properties);
                return(item);
            }
        }