/// <summary> /// Ons the message. /// </summary> /// <param name="message">Message.</param> private void OnMessage(EasyCachingMessage message) { // each clients will recive the message, current client should ignore. if (!string.IsNullOrWhiteSpace(message.Id) && message.Id.Equals(_cacheId, StringComparison.OrdinalIgnoreCase)) { return; } //remove by prefix if (message.IsPrefix) { var prefix = message.CacheKeys.First(); _localCache.RemoveByPrefix(prefix); if (_options.EnableLogging) { _logger.LogTrace($"remove local cache that prefix is {prefix}"); } return; } foreach (var item in message.CacheKeys) { _localCache.Remove(item); if (_options.EnableLogging) { _logger.LogTrace($"remove local cache that cache key is {item}"); } } }
/// <summary> /// Removes cached item by cachekey's prefix. /// </summary> /// <returns>The by prefix async.</returns> /// <param name="prefix">Prefix.</param> public void RemoveByPrefix(string prefix) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); _localCachingProvider.RemoveByPrefix(prefix); _distributedCachingProvider.RemoveByPrefix(prefix); }
private void DeviceDisconnectedHandler(IBleAdapter sender, BleDeviceEventArgs args) { var device = args.Device; var deviceId = device.Id; lock (lockObject) { Devices.Remove(deviceId); _cachingProvider.RemoveByPrefix(DiscoveredDeviceCachePrefix); } }
public void ClearRegion(string region) { if (!_options.EnableHybrid) { _provider.RemoveByPrefix(region); } else { _hybridProvider.RemoveByPrefix(region); } }
/// <summary> /// Removes the by prefix. /// </summary> /// <param name="prefix">Prefix.</param> public void RemoveByPrefix(string prefix) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); //distributed cache at first _distributedCache.RemoveByPrefix(prefix); _localCache.RemoveByPrefix(prefix); //send message to bus _bus.Publish(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { prefix }, IsPrefix = true }); }
/// <summary> /// Removes the by prefix. /// </summary> /// <param name="prefix">Prefix.</param> public void RemoveByPrefix(string prefix) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); try { _distributedCache.RemoveByPrefix(prefix); } catch (Exception ex) { LogMessage($"remove by prefix [{prefix}] error", ex); } _localCache.RemoveByPrefix(prefix); // send message to bus _busSyncWrap.Execute(() => _bus.Publish(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { prefix }, IsPrefix = true })); }
/// <summary> /// Processes the evict. /// </summary> /// <param name="invocation">Invocation.</param> /// <param name="isBefore">If set to <c>true</c> is before.</param> private void ProcessEvict(IInvocation invocation, bool isBefore) { var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget; if (serviceMethod.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(EasyCachingEvictAttribute)) is EasyCachingEvictAttribute attribute && attribute.IsBefore == isBefore) { if (attribute.IsAll) { //If is all , clear all cached items which cachekey start with the prefix. var cacheKeyPrefix = _keyGenerator.GetCacheKeyPrefix(serviceMethod, attribute.CacheKeyPrefix); _cacheProvider.RemoveByPrefix(cacheKeyPrefix); } else { //If not all , just remove the cached item by its cachekey. var cacheKey = _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix); _cacheProvider.Remove(cacheKey); } } }
/// <summary> /// Processes the evict. /// </summary> /// <param name="invocation">Invocation.</param> /// <param name="isBefore">If set to <c>true</c> is before.</param> private void ProcessEvict(IInvocation invocation, bool isBefore) { var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget; if (GetMethodAttributes(serviceMethod).FirstOrDefault(x => x.GetType() == typeof(EasyCachingEvictAttribute)) is EasyCachingEvictAttribute attribute && attribute.IsBefore == isBefore) { try { if (attribute.IsAll) { //If is all , clear all cached items which cachekey start with the prefix. var cacheKeyPrefix = _keyGenerator.GetCacheKeyPrefix(serviceMethod, attribute.CacheKeyPrefix); _cacheProvider.RemoveByPrefix(cacheKeyPrefix); } else { //If not all , just remove the cached item by its cachekey. var cacheKey = _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix); _cacheProvider.Remove(cacheKey); } } catch (Exception ex) { if (!attribute.IsHightAvailability) { throw; } else { _logger?.LogError(new EventId(), ex, $"Cache provider \"{_cacheProvider.Name}\" remove error."); } } } }
/// <summary> /// Removes items by key prefix /// </summary> /// <param name="prefix">String key prefix</param> public void RemoveByPrefix(string prefix) { _provider.RemoveByPrefix(prefix); }
public void RemoveByPrefix_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string prefix) { Assert.Throws <ArgumentNullException>(() => _provider.RemoveByPrefix(prefix)); }
/// <summary> /// Removes cached item by cachekey's prefix. /// </summary> /// <param name="prefix">Prefix of CacheKey.</param> public void RemoveByPrefix(string prefix) { _easyCachingProvider.RemoveByPrefix(prefix); }
/// <summary> /// Removes items by key prefix /// </summary> /// <param name="prefix">String key prefix</param> public void RemoveByPrefix(string prefix) { _provider.RemoveByPrefix(prefix.Trim().ToLower()); }