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

nshkrdotcom / ElixirScope / a3fb310680b32807957a42335caaaea14df2cc5d

29 May 2025 04:40AM UTC coverage: 58.763% (-0.1%) from 58.866%
a3fb310680b32807957a42335caaaea14df2cc5d

push

github

NSHkr
refactor pattern matcher

77 of 218 new or added lines in 10 files covered. (35.32%)

18 existing lines in 2 files now uncovered.

5918 of 10071 relevant lines covered (58.76%)

3267.44 hits per line

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

0.0
/lib/elixir_scope/ast_repository/pattern_matcher/cache.ex
1
defmodule ElixirScope.ASTRepository.PatternMatcher.Cache do
2
  @moduledoc """
3
  Caching layer for pattern matching results to improve performance.
4
  """
5
  
6
  use GenServer
7
  require Logger
8
  
9
  alias ElixirScope.ASTRepository.PatternMatcher.Config
10
  
11
  @cache_table :pattern_match_cache
12
  
13
  def start_link(opts \\ []) do
NEW
14
    GenServer.start_link(__MODULE__, opts, name: __MODULE__)
×
15
  end
16
  
17
  def init(_opts) do
NEW
18
    if Config.get(:enable_pattern_cache) do
×
NEW
19
      :ets.new(@cache_table, [:named_table, :public, :set, {:read_concurrency, true}])
×
NEW
20
      schedule_cleanup()
×
21
    end
22
    
23
    {:ok, %{cleanup_timer: nil}}
24
  end
25
  
26
  @spec get(term()) :: {:ok, term()} | :not_found
27
  def get(key) do
NEW
28
    if Config.get(:enable_pattern_cache) do
×
NEW
29
      case :ets.lookup(@cache_table, key) do
×
30
        [{^key, value, timestamp}] ->
NEW
31
          if cache_expired?(timestamp) do
×
NEW
32
            :ets.delete(@cache_table, key)
×
33
            :not_found
34
          else
35
            {:ok, value}
36
          end
NEW
37
        [] ->
×
38
          :not_found
39
      end
40
    else
41
      :not_found
42
    end
43
  end
44
  
45
  @spec put(term(), term()) :: :ok
46
  def put(key, value) do
NEW
47
    if Config.get(:enable_pattern_cache) do
×
NEW
48
      timestamp = System.system_time(:second)
×
NEW
49
      :ets.insert(@cache_table, {key, value, timestamp})
×
50
    end
51
    :ok
52
  end
53
  
54
  @spec clear() :: :ok
55
  def clear do
NEW
56
    if Config.get(:enable_pattern_cache) do
×
NEW
57
      :ets.delete_all_objects(@cache_table)
×
58
    end
59
    :ok
60
  end
61
  
62
  def handle_info(:cleanup_expired, state) do
NEW
63
    cleanup_expired_entries()
×
NEW
64
    schedule_cleanup()
×
65
    {:noreply, state}
66
  end
67
  
68
  defp cache_expired?(timestamp) do
NEW
69
    ttl_seconds = Config.get(:cache_ttl_minutes) * 60
×
NEW
70
    System.system_time(:second) - timestamp > ttl_seconds
×
71
  end
72
  
73
  defp schedule_cleanup do
74
    # Run cleanup every 5 minutes
NEW
75
    Process.send_after(self(), :cleanup_expired, 5 * 60 * 1000)
×
76
  end
77
  
78
  defp cleanup_expired_entries do
NEW
79
    if Config.get(:enable_pattern_cache) do
×
NEW
80
      current_time = System.system_time(:second)
×
NEW
81
      ttl_seconds = Config.get(:cache_ttl_minutes) * 60
×
82
      
NEW
83
      expired_keys = :ets.select(@cache_table, [
×
84
        {{'$1', '_', '$3'}, [{:'<', '$3', current_time - ttl_seconds}], ['$1']}
85
      ])
86
      
NEW
87
      Enum.each(expired_keys, fn key ->
×
NEW
88
        :ets.delete(@cache_table, key)
×
89
      end)
90
      
NEW
91
      if length(expired_keys) > 0 do
×
NEW
92
        Logger.debug("Cleaned up #{length(expired_keys)} expired cache entries")
×
93
      end
94
    end
95
  end
96
end
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