/// <summary> /// Формирует значение кеша для города /// </summary> /// <param name="city">Название города</param> /// <param name="startIndex">Начало интервала</param> /// <param name="endIndex">Конец интервала</param> /// <returns></returns> private byte[] AddCityCacheEntry(string city, int startIndex, int endIndex) { byte[] result; if (_cityCache.TryGetValue(city, out result)) { return(result); } int bufferSize = 1; for (int i = startIndex; i <= endIndex; i++) { bufferSize += _locationCache[_db.GetLocationIdByNameIndex(i)].Length + 1; } JsonSerializer.Buffer buffer = new JsonSerializer.Buffer(bufferSize); JsonSerializer.StartArray(buffer); for (int i = startIndex; i <= endIndex; i++) { if (i > startIndex) { JsonSerializer.Comma(buffer); } buffer.Write(_locationCache[_db.GetLocationIdByNameIndex(i)]); } JsonSerializer.EndArray(buffer); result = buffer.GetByteArray(); _cityCache.TryAdd(city, result); return(result); }
/// <summary> /// Собирает кеш для города и возвращает его значение /// </summary> /// <param name="city">Название города</param> /// <param name="firstElement">Первый элемент в индексе по названию города</param> /// <param name="lastElement">Последний элемент в индексе по названию города</param> /// <returns>Возвращает ответ, готовый к отправке по сети</returns> private byte[] AcquireCityResult(string city, int firstElement, int lastElement) { if (!_locationCacheIsLoaded) { JsonSerializer.Buffer buffer = null; for (int i = firstElement; i <= lastElement; i++) { byte[] result; int locationIndex = _db.GetLocationIdByNameIndex(i); Thread.MemoryBarrier(); if (_locationCache[locationIndex] == null) { if (buffer == null) { buffer = JsonSerializer.GetBufferForLocation(); } result = GetLocationJson(locationIndex, buffer); _locationCache[locationIndex] = result; } } } return(AddCityCacheEntry(city, firstElement, lastElement)); }
/// <summary> /// Возвращает информацию о местоположении по IP-адресу /// </summary> /// <param name="ip">IP-адрес</param> /// <returns>Возвращает ответ, готовый к отправке по сети</returns> internal unsafe byte[] GetLocationByIp(uint ip) { if (!_isDataAvailable) { return(null); } byte[] result = null; uint locationIndex; if (_db.GetLocationIndexByIp(ip, out locationIndex)) { if (!_isCacheAvailable) { Thread.MemoryBarrier(); result = _locationCache[(int)locationIndex]; if (result == null) { JsonSerializer.Buffer buffer = JsonSerializer.GetBufferForLocation(); result = GetLocationJson((int)locationIndex, buffer); _locationCache[locationIndex] = result; } } else { return(_locationCache[locationIndex]); } } return(result); }
/// <summary> /// Формирует значение кеша местоположения /// </summary> /// <param name="i">Идентификатор местоположения</param> /// <param name="buffer">Буфер</param> /// <returns></returns> private unsafe byte[] GetLocationJson(int i, JsonSerializer.Buffer buffer) { buffer.Reset(); LocationRecord rec = _db.GetLocationRecordById(i); JsonSerializer.WriteLocationRecord(buffer, rec); return(buffer.GetByteArray()); }