• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microsoft / RulesEngine / 5671364341

26 Jul 2023 04:33PM UTC coverage: 96.44% (-2.0%) from 98.42%
5671364341

push

github

web-flow
Bump coverallsapp/github-action from 1.1.3 to 2.2.1 (#504)

Bumps [coverallsapp/github-action](https://github.com/coverallsapp/github-action) from 1.1.3 to 2.2.1.
- [Release notes](https://github.com/coverallsapp/github-action/releases)
- [Upgrade guide](https://github.com/coverallsapp/github-action/blob/main/UPGRADE.md)
- [Commits](https://github.com/coverallsapp/github-action/compare/1.1.3...v2.2.1)

---
updated-dependencies:
- dependency-name: coverallsapp/github-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

294 of 354 branches covered (83.05%)

623 of 646 relevant lines covered (96.44%)

94.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.32
/src/RulesEngine/HelperFunctions/MemCache.cs
1
// Copyright (c) Microsoft Corporation.
2
// Licensed under the MIT License.
3

4
using System;
5
using System.Collections;
6
using System.Collections.Concurrent;
7
using System.Collections.Generic;
8

9
namespace RulesEngine.HelperFunctions
10
{
11
    public class MemCacheConfig {
12
        public int SizeLimit { get; set; } = 1000;
154✔
13
    }
14

15

16
    internal class MemCache
17
    {
18
        private readonly MemCacheConfig _config;
19
        private ConcurrentDictionary<string, (object value, DateTimeOffset expiry)> _cacheDictionary;
20
        private ConcurrentQueue<(string key, DateTimeOffset expiry)> _cacheEvictionQueue;
21

22
        public MemCache(MemCacheConfig config)
85✔
23
        {
24
            if(config == null)
85!
25
            {
26
                config = new MemCacheConfig();
×
27
            }
28
            _config = config;
85✔
29
            _cacheDictionary = new ConcurrentDictionary<string, (object value, DateTimeOffset expiry)>();
85✔
30
            _cacheEvictionQueue = new ConcurrentQueue<(string key, DateTimeOffset expiry)>();
85✔
31
        }
85✔
32

33
        public bool TryGetValue<T>(string key,out T value)
34
        {
35
            value = default;
147✔
36
            if (_cacheDictionary.TryGetValue(key, out var cacheItem))
147✔
37
            {
38
                if(cacheItem.expiry < DateTimeOffset.UtcNow)
74!
39
                {
40
                    _cacheDictionary.TryRemove(key, out _);
×
41
                    return false;
×
42
                }
43
                else
44
                {
45
                    value = (T)cacheItem.value;
74✔
46
                    return true;
74✔
47
                }   
48
            }
49
            return false;
73✔
50
           
51
        }
52

53

54
        public T Get<T>(string key)
55
        {
56
            TryGetValue<T>(key, out var value);
71✔
57
            return value;
71✔
58
        }
59

60

61
        /// <summary>
62
        /// Returns all known keys. May return keys for expired data as well
63
        /// </summary>
64
        /// <returns></returns>
65
        public IEnumerable<string> GetKeys()
66
        {
67
            return _cacheDictionary.Keys;
5✔
68
        }
69

70
        public T GetOrCreate<T>(string key, Func<T> createFn, DateTimeOffset? expiry = null)
71
        {
72
            if(!TryGetValue<T>(key,out var value))
×
73
            {
74
                value = createFn();
×
75
                return Set<T>(key,value,expiry);
×
76
            }
77
            return value;
×
78
        }
79

80
        public T Set<T>(string key, T value, DateTimeOffset? expiry = null)
81
        {
82
            var fixedExpiry = expiry ?? DateTimeOffset.MaxValue;
69!
83

84
            while (_cacheDictionary.Count > _config.SizeLimit)
69!
85
            {
86
                if (_cacheEvictionQueue.IsEmpty)
×
87
                {
88
                    _cacheDictionary.Clear();
×
89
                }
90
                if(_cacheEvictionQueue.TryDequeue(out var result)
×
91
                    && _cacheDictionary.TryGetValue(result.key,out var dictionaryValue)
×
92
                    &&  dictionaryValue.expiry == result.expiry)
×
93
                {   
94
                    _cacheDictionary.TryRemove(result.key, out _);
×
95
                }
96
                
97
            }
98

99
            _cacheDictionary.AddOrUpdate(key, (value, fixedExpiry), (k, v) => (value, fixedExpiry));
70✔
100
            _cacheEvictionQueue.Enqueue((key, fixedExpiry));
69✔
101
            return value;
69✔
102
        }
103

104
        public void Remove(string key)
105
        {
106
            _cacheDictionary.TryRemove(key, out _);
5✔
107
        }
5✔
108

109
        public void Clear()
110
        {
111
            _cacheDictionary.Clear();
2✔
112
            _cacheEvictionQueue =  new ConcurrentQueue<(string key, DateTimeOffset expiry)>();
2✔
113
        }
2✔
114
    }
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc