Esempio n. 1
0
        public byte[] GetOrExpire(string key, TimeSpan s)
        {
            var ticks = DateTime.UtcNow.Subtract(s).Ticks;

            var  keybytes = Encoding.UTF8.GetBytes(key);
            uint hash     = FNV64.Compute(keybytes);

            uint _pos;

            if (!GetKeySlot(hash, keybytes, out _pos))
            {
                return(null);
            }
            var i = (int)_pos;

            if (_index.GetDataCreateTime(i) < ticks)
            {
                var key_sector = _index.GetKeySector(i);

                _storage.Delete(key_sector);
                _index.SetKeySector(i, 0);
                _storage.Delete(_index.GetDataSector(i));
                _index.used -= 1;
                _index.Flush();
                return(null);
            }
            else
            {
                return(_storage.Retrieve(_index.GetDataSector(i)));
            }
        }
Esempio n. 2
0
        public byte[] Get(string key)
        {
            var  keybytes = Encoding.UTF8.GetBytes(key);
            uint hash     = FNV64.Compute(keybytes);

            uint _pos;

            if (!GetKeySlot(hash, keybytes, out _pos))
            {
                return(null);
            }

            int pos = (int)_pos;

            byte[] result = _storage.Retrieve(_index.GetDataSector(pos));
            return(result);
        }
Esempio n. 3
0
        public void Remove(string key)
        {
            var  keybytes = Encoding.UTF8.GetBytes(key);
            uint hash     = FNV64.Compute(keybytes);

            uint pos;

            if (!GetKeySlot(hash, keybytes, out pos))
            {
                return;
            }

            int _pos = (int)pos;

            _storage.Delete(_index.GetKeySector(_pos));
            _index.SetKeySector(_pos, 0);
            _storage.Delete(_index.GetDataSector(_pos));
            _index.used -= 1;
            _index.Flush();
        }
Esempio n. 4
0
        /// <summary>
        /// store a key in collection
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">value</param>
        /// <returns></returns>
        public bool Put(string key, byte[] data)
        {
            var  keybytes = Encoding.UTF8.GetBytes(key);
            uint hash     = FNV64.Compute(keybytes);

            if ((double)_index.fill / (double)_index.mask > 0.65)
            {
                _index.Grow();
            }

            uint _pos;

            if (GetKeySlot(hash, keybytes, out _pos))
            {
                // update existing slot
                int pos = (int)_pos;
                _storage.Delete(_index.GetDataSector(pos));
                _index.SetDataCreateTime(pos, DateTime.UtcNow.Ticks);
                _index.SetDataSector(pos, _storage.Store(data));
            }
            else
            {
                int pos = (int)_pos;
                _index.SetHash(pos, hash);
                _index.SetKeySector(pos, _storage.Store(keybytes));
                _index.SetDataCreateTime(pos, DateTime.UtcNow.Ticks);
                _index.SetDataSector(pos, _storage.Store(data));

                _index.fill += 1;
                _index.used += 1;
            }

            _index.Flush();

            return(true);
        }