Module: PlanMyStuff::Cache

Defined in:
lib/plan_my_stuff/cache.rb

Overview

ETag-backed HTTP cache for GitHub REST reads.

Stores ‘{ etag:, body: }` pairs in `Rails.cache` keyed by resource. Readers look up the entry, pass its ETag in `If-None-Match`, and either (a) get a cheap 304 and serve the cached body, or (b) get a fresh 200, write the new ETag + body, and return that. Writes from .create!/.update! front-load the cache with the fresh ETag returned on the mutating response so the next read is cheap too.

Covers issues, comments, and their list endpoints.

Constant Summary collapse

CACHE_VERSION =

Gem-internal cache version. Bump when the cache layout or the payload shape changes in a backwards-incompatible way so existing entries are orphaned rather than mis-read.

'v1'

Class Method Summary collapse

Class Method Details

.delete_comment(repo, id) ⇒ void

This method returns an undefined value.

Removes the cache entry for a comment.

Parameters:

  • repo (String)
  • id (Integer)


114
115
116
117
118
# File 'lib/plan_my_stuff/cache.rb', line 114

def delete_comment(repo, id)
  return unless enabled?

  ::Rails.cache.delete(comment_key(repo, id))
end

.delete_issue(repo, number) ⇒ void

This method returns an undefined value.

Removes the cache entry for an issue.

Parameters:

  • repo (String)
  • number (Integer)


69
70
71
72
73
# File 'lib/plan_my_stuff/cache.rb', line 69

def delete_issue(repo, number)
  return unless enabled?

  ::Rails.cache.delete(issue_key(repo, number))
end

.enabled?Boolean

Returns whether ‘Rails.cache` is available and caching is enabled.

Returns:

  • (Boolean)

    whether ‘Rails.cache` is available and caching is enabled



23
24
25
26
27
28
# File 'lib/plan_my_stuff/cache.rb', line 23

def enabled?
  return false unless PlanMyStuff.configuration.cache_enabled
  return false if !defined?(::Rails) || !::Rails.respond_to?(:cache) || ::Rails.cache.nil?

  true
end

.read_comment(repo, id) ⇒ Hash{Symbol => Object}?

Reads the cached ‘body:` entry for a comment.

Parameters:

  • repo (String)

    resolved full repo name (e.g. “Org/Name”)

  • id (Integer)

Returns:

  • (Hash{Symbol => Object}, nil)


82
83
84
85
86
# File 'lib/plan_my_stuff/cache.rb', line 82

def read_comment(repo, id)
  return unless enabled?

  ::Rails.cache.read(comment_key(repo, id))
end

.read_issue(repo, number) ⇒ Hash{Symbol => Object}?

Reads the cached ‘body:` entry for an issue.

Parameters:

  • repo (String)

    resolved full repo name (e.g. “Org/Name”)

  • number (Integer)

Returns:

  • (Hash{Symbol => Object}, nil)


37
38
39
40
41
# File 'lib/plan_my_stuff/cache.rb', line 37

def read_issue(repo, number)
  return unless enabled?

  ::Rails.cache.read(issue_key(repo, number))
end

.read_list(resource, repo, params) ⇒ Hash{Symbol => Object}?

Reads the cached ‘body:` entry for a list endpoint.

Parameters:

  • resource (Symbol)

    :issue or :comment

  • repo (String)

    resolved full repo name

  • params (Hash)

    query params that make this list unique

Returns:

  • (Hash{Symbol => Object}, nil)


128
129
130
131
132
# File 'lib/plan_my_stuff/cache.rb', line 128

def read_list(resource, repo, params)
  return unless enabled?

  ::Rails.cache.read(list_key(resource, repo, params))
end

.write_comment(repo, id, etag:, body:) ⇒ void

This method returns an undefined value.

Writes a cache entry for a comment.

Parameters:

  • repo (String)

    resolved full repo name

  • id (Integer)
  • etag (String, nil)

    value of the ‘ETag` response header

  • body (Object)

    parsed GitHub comment response



97
98
99
100
101
102
103
104
105
# File 'lib/plan_my_stuff/cache.rb', line 97

def write_comment(repo, id, etag:, body:)
  return unless enabled?
  return if etag.blank?

  ::Rails.cache.write(
    comment_key(repo, id),
    { etag: etag, body: normalize(body) },
  )
end

.write_issue(repo, number, etag:, body:) ⇒ void

This method returns an undefined value.

Writes a cache entry for an issue.

Parameters:

  • repo (String)

    resolved full repo name

  • number (Integer)
  • etag (String, nil)

    value of the ‘ETag` response header

  • body (Object)

    parsed GitHub issue response



52
53
54
55
56
57
58
59
60
# File 'lib/plan_my_stuff/cache.rb', line 52

def write_issue(repo, number, etag:, body:)
  return unless enabled?
  return if etag.blank?

  ::Rails.cache.write(
    issue_key(repo, number),
    { etag: etag, body: normalize(body) },
  )
end

.write_list(resource, repo, params, etag:, body:) ⇒ void

This method returns an undefined value.

Writes a cache entry for a list endpoint.

Parameters:

  • resource (Symbol)

    :issue or :comment

  • repo (String)

    resolved full repo name

  • params (Hash)

    query params that make this list unique

  • etag (String, nil)

    value of the ‘ETag` response header

  • body (Object)

    parsed GitHub list response (array of resources)



144
145
146
147
148
149
150
151
152
# File 'lib/plan_my_stuff/cache.rb', line 144

def write_list(resource, repo, params, etag:, body:)
  return unless enabled?
  return if etag.blank?

  ::Rails.cache.write(
    list_key(resource, repo, params),
    { etag: etag, body: normalize(body) },
  )
end