Exemplo n.º 1
0
        /// <summary>
        /// Removes an object from the cache.
        /// </summary>
        /// <param name="id">The id of the object to remove.</param>
        /// <param name="cacheType">The cache type to remove from</param>
        /// <returns>Object successfuly removed</returns>
        public bool Remove(uint?id, CacheType cacheType)
        {
            if (id == null)
            {
                return(false);
            }

            if (_cached_objects.ContainsKey((uint)id))
            {
                // Call CacheDestroy to trigger event
                var entity = _cached_objects[(uint)id];
                ((CacheableObject)entity).CacheDestroy((uint)id, cacheType);

                // Remove from cache
                _cached_objects.Remove((uint)id);
                _id_list.Remove((uint)id);

                if (cacheType == CacheType.Prototype)
                {
                    DataPersistence.DeleteObject(entity);
                }

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an object to the cache. You MUST first check to see if the cache contains a duplicate key.
        /// </summary>
        /// <param name="entity">The object to add.</param>
        /// <param name="specificID">The specific ID to add. If -1, find first available ID.</param>
        /// <returns>The ID of the added object</returns>
        public uint Add(ICacheableObject entity, CacheType cacheType, uint?specificID = null, bool persist = false)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(Add));
            }

            uint id;

            // Consume the appropriate ID
            if (specificID == null)
            {
                id = ConsumeFirstAvailableID();
            }
            else
            {
                id = ConsumeSpecifiedID((uint)specificID);
            }

            _cached_objects.Add(id, entity);

            if (cacheType == CacheType.Instance)
            {
                entity.Instance = id;
            }
            else
            {
                entity.Prototype = id;
            }

            entity.CacheType = cacheType;

            if (persist)
            {
                DataPersistence.SaveObject(entity);
            }

            return(id);
        }