Class: Twterm::Repository::AbstractExpirableEntityRepository

Inherits:
AbstractEntityRepository show all
Includes:
Publisher
Defined in:
lib/twterm/repository/abstract_expirable_entity_repository.rb

Direct Known Subclasses

StatusRepository, UserRepository

Instance Method Summary collapse

Methods included from Publisher

#publish

Methods included from Utils

check_type

Methods inherited from AbstractEntityRepository

#all

Methods inherited from AbstractRepository

#after_create, #before_create, #type

Constructor Details

#initializeAbstractExpirableEntityRepository

Returns a new instance of AbstractExpirableEntityRepository.



11
12
13
14
# File 'lib/twterm/repository/abstract_expirable_entity_repository.rb', line 11

def initialize
  super
  @touched_at = Concurrent::Hash.new
end

Instance Method Details

#create(args) ⇒ Object



16
17
18
19
# File 'lib/twterm/repository/abstract_expirable_entity_repository.rb', line 16

def create(args, *)
  touch(args.id)
  super
end

#expire(threshold) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/twterm/repository/abstract_expirable_entity_repository.rb', line 28

def expire(threshold)
  now = Time.now
  ids = repository.select { |id, _| !@touched_at[id] || @touched_at[id] + threshold < now }

  ids.each { |id| publish(garbage_collection_event_class.new(id)) }

  repository.delete_if { |id, _| ids.include?(id) }
end

#find(id) ⇒ Object



21
22
23
24
25
26
# File 'lib/twterm/repository/abstract_expirable_entity_repository.rb', line 21

def find(id)
  instance = super
  touch(id) if !instance.nil? && should_keep?(instance)

  instance
end