コード例 #1
0
    public void Subscribe()
    {
        if (!ConfigurationVerifier
            .TryVerifyConfiguration(
                "notify-keyspace-events",
                out var keyeventNotificationsException,
                "g",
                "h",
                "K"))
        {
            Logger?.LogWarning(
                keyeventNotificationsException,
                "Failed to verify keyspace notifications config.");
        }

        Subscriber.Value.Subscribe(
            "__keyspace@*__:*",
            (channel, message) =>
        {
            if (message == "del" ||
                message == "hset")
            {
                var keyPrefixIndex = channel.ToString().IndexOf(
                    L1L2RedisCacheOptions.KeyPrefix);
                if (keyPrefixIndex != -1)
                {
                    var key = channel.ToString()[
                        (keyPrefixIndex + L1L2RedisCacheOptions.KeyPrefix.Length)..];
                    L1Cache.Remove(
                        $"{L1L2RedisCacheOptions.KeyPrefix}{key}");
                    L1Cache.Remove(
                        $"{L1L2RedisCacheOptions.LockKeyPrefix}{key}");
                }
コード例 #2
0
    public void Subscribe()
    {
        if (!ConfigurationVerifier
            .TryVerifyConfiguration(
                "notify-keyspace-events",
                out var keyeventNotificationsException,
                "g",
                "h",
                "E"))
        {
            Logger?.LogWarning(
                keyeventNotificationsException,
                "Failed to verify keyevent notifications config.");
        }

        Subscriber.Value.Subscribe(
            "__keyevent@*__:del",
            (channel, message) =>
        {
            if (message.StartsWith(
                    L1L2RedisCacheOptions.KeyPrefix))
            {
                var key = message
                          .ToString()[L1L2RedisCacheOptions.KeyPrefix.Length..];
                L1Cache.Remove(
                    $"{L1L2RedisCacheOptions.KeyPrefix}{key}");
                L1Cache.Remove(
                    $"{L1L2RedisCacheOptions.LockKeyPrefix}{key}");
            }
コード例 #3
0
        public virtual async Task Set(string key, object value, CachePolicyItem cachePolicy)
        {
            //清除一级缓存,写入二级缓存
            await L1Cache.Remove(key);

            await L2Cache.Set(key, value, cachePolicy);

            //后台写入一级缓存。
            Background(L1Cache.Set(key, value, cachePolicy));
        }
コード例 #4
0
    /// <summary>
    /// Removes the value with the given key.
    /// </summary>
    /// <param name="key">A string identifying the requested value.</param>
    public void Remove(string key)
    {
        var semaphore = GetOrCreateLock(key, null);

        semaphore.Wait();
        try
        {
            L2Cache.Remove(key);
            L1Cache.Remove(
                $"{L1L2RedisCacheOptions.KeyPrefix}{key}");
            MessagePublisher.Publish(key);
            L1Cache.Remove(
                $"{L1L2RedisCacheOptions.LockKeyPrefix}{key}");
        }
        finally
        {
            semaphore.Release();
        }
    }
コード例 #5
0
 public void Subscribe()
 {
     Subscriber.Value.Subscribe(
         L1L2RedisCacheOptions.Channel,
         (channel, message) =>
     {
         var cacheMessage = JsonSerializer
                            .Deserialize <CacheMessage>(
             message.ToString(),
             JsonSerializerOptions);
         if (cacheMessage?.PublisherId !=
             L1L2RedisCacheOptions.Id)
         {
             L1Cache.Remove(
                 $"{L1L2RedisCacheOptions.KeyPrefix}{cacheMessage?.Key}");
             L1Cache.Remove(
                 $"{L1L2RedisCacheOptions.LockKeyPrefix}{cacheMessage?.Key}");
         }
     });
 }
コード例 #6
0
    /// <summary>
    /// Removes the value with the given key.
    /// </summary>
    /// <param name="key">A string identifying the requested value.</param>
    /// <param name="cancellationToken">Optional. The System.Threading.CancellationToken used to propagate notifications that the operation should be canceled.</param>
    /// <returns>The System.Threading.Tasks.Task that represents the asynchronous operation.</returns>
    public async Task RemoveAsync(
        string key,
        CancellationToken cancellationToken = default)
    {
        var semaphore = await GetOrCreateLockAsync(
            key, null, cancellationToken);

        await semaphore.WaitAsync(cancellationToken);

        try
        {
            L2Cache.Remove(key);
            L1Cache.Remove(
                $"{L1L2RedisCacheOptions.KeyPrefix}{key}");
            MessagePublisher.Publish(key);
            L1Cache.Remove(
                $"{L1L2RedisCacheOptions.LockKeyPrefix}{key}");
        }
        finally
        {
            semaphore.Release();
        }
    }
コード例 #7
0
 public virtual Task Remove(string cacheKey)
 {
     return(Task.WhenAll(L1Cache.Remove(cacheKey), L2Cache.Remove(cacheKey)));
 }