Exemplo n.º 1
0
        public void Add(OnlineMapsCache cache, OnlineMapsTile tile, byte[] bytes)
        {
#if ALLOW_FILECACHE
            StringBuilder filename      = cache.GetShortTilePath(tile);
            string        shortFilename = filename.ToString();
            if (Contains(shortFilename))
            {
                return;
            }

            string fullFilename = cache.GetFullTilePath(shortFilename);

            OnlineMapsThreadManager.AddThreadAction(() =>
            {
                FileInfo fileInfo = new FileInfo(fullFilename);
                if (!Directory.Exists(fileInfo.DirectoryName))
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }
                File.WriteAllBytes(fullFilename, bytes);
            });

            AddItem(shortFilename, bytes.Length);
            _size += bytes.Length;
#endif
        }
        public void DeleteOldItems(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            int countUnload = Mathf.RoundToInt(count * cache.fileCacheUnloadRate);
            if (countUnload <= 0)
            {
                throw new Exception("Can not unload a negative number of items. Check fileCacheUnloadRate.");
            }
            if (count < countUnload)
            {
                countUnload = count;
            }

            long[] unloadTimes   = new long[countUnload];
            int[]  unloadIndices = new int[countUnload];
            int    c             = 0;

            for (int i = 0; i < count; i++)
            {
                long t = items[i].time;
                if (c == 0)
                {
                    unloadIndices[0] = 0;
                    unloadTimes[0]   = t;
                    c++;
                }
                else
                {
                    int index  = c;
                    int index2 = index - 1;

                    while (index2 >= 0)
                    {
                        if (unloadTimes[index2] < t)
                        {
                            break;
                        }

                        index2--;
                        index--;
                    }

                    if (index < countUnload)
                    {
                        for (int j = countUnload - 1; j > index; j--)
                        {
                            unloadIndices[j] = unloadIndices[j - 1];
                            unloadTimes[j]   = unloadTimes[j - 1];
                        }
                        unloadIndices[index] = i;
                        unloadTimes[index]   = t;
                        if (c < countUnload)
                        {
                            c++;
                        }
                    }
                }
            }

            for (int i = 0; i < countUnload; i++)
            {
                int index = unloadIndices[i];
                _size -= items[index].size;
                string fullFilename = cache.GetFullTilePath(items[index].filename);
                if (File.Exists(fullFilename))
                {
                    File.Delete(fullFilename);
                }
                items[index] = null;
            }

            int offset = 0;
            for (int i = 0; i < count; i++)
            {
                if (items[i] == null)
                {
                    offset++;
                }
                else if (offset > 0)
                {
                    items[i - offset] = items[i];
                }
            }

            count -= countUnload;
#endif
        }