Module: FlagpoleSitta::CacheSitta::ClassMethods

Defined in:
lib/flagpole_sitta/cache_sitta.rb

Instance Method Summary collapse

Instance Method Details

#array_cache_key_gen(key, route_id, options = {}) ⇒ Object

Options :emptystack will make it generate a key for the emptystack instead of the general cache array.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flagpole_sitta/cache_sitta.rb', line 33

def array_cache_key_gen key, route_id, options={}

  mid_key = mid_key_gen route_id

  model = options[:model] || clazz


  if options[:emptystack]
    "#{model}/#{mid_key}/EmptyStack/#{key}"
  else
    "#{model}/#{mid_key}/#{key}"
  end

end

#clazzObject



19
20
21
# File 'lib/flagpole_sitta/cache_sitta.rb', line 19

def clazz
  self
end

#destroy_array_cache(options = {}) ⇒ Object

Nukes all corresponding caches for a given array.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/flagpole_sitta/cache_sitta.rb', line 124

def destroy_array_cache options={}

  each_cache options[:route_id] do |hash|
    #A Check in Case there is some type of cache failure, or it is an empty spot, also if it has no scope, or it falls in scope
    if hash.present? && (hash[:scope].nil? || options[:obj].in_scope(hash[:scope]))
      #Get all the associated.
      associated = FlagpoleSitta::CommonFs.flagpole_cache_read(hash[:key])[:associated]
      Rails.logger.info "#{hash[:key]} is being cleared"
      #Destroy the actually cache
      FlagpoleSitta::CommonFs.flagpole_cache_delete(hash[:key])
      #The associated objects will always include the object we got the actually cache from
      associated.each do |a|
        #Get the base key
        base_key = a.gsub(/\/[^\/]*\z/, "")
        #Get the flag. Capture the god damn flag!
        flag_key = base_key + "/Flag"
        #Get its location in the 'Array'
        n = a.split("/").last
        # Check in case of cache failure
        if flag = FlagpoleSitta::CommonFs.flagpole_cache_read(flag_key)
          #Add an empty spot to the 'Array'
          flag[:empty] = flag[:empty] + 1
          empty_stack_key = base_key + "/EmptyStack/" + flag[:empty].to_s
          #Save the empty spot location to the 'Stack'
          FlagpoleSitta::CommonFs.flagpole_cache_write(empty_stack_key, n)
          #Update the flag
          FlagpoleSitta::CommonFs.flagpole_cache_write(flag_key, flag)
        end

        #Finally get rid of the associated cache object.
        FlagpoleSitta::CommonFs.flagpole_cache_delete(a)
      end
    #Else It is not in scope so the cache lives to fight another day!
    end
  end

end

#each_cache(route_id = nil, &block) ⇒ Object

Loops through the array in the cache.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/flagpole_sitta/cache_sitta.rb', line 102

def each_cache route_id = nil, &block

  flag_key = array_cache_key_gen "Flag", route_id

  flag = FlagpoleSitta::CommonFs.flagpole_cache_read(flag_key)

  #AR - If there aren't any index do nothing.
  #Else wise loop through every index.
  #If it actually does exist then yield.
  if flag
    for i in 0..flag[:space] do
      array_key = array_cache_key_gen i, route_id
      hash = FlagpoleSitta::CommonFs.flagpole_cache_read(array_key)
      yield hash
    end
  end

  nil

end

#initialize_array_cache(route_id = nil) ⇒ Object

Creates the ‘array’ in the cache.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/flagpole_sitta/cache_sitta.rb', line 49

def initialize_array_cache route_id = nil

  flag_key = array_cache_key_gen "Flag", route_id

  flag = {:space => -1, :empty => -1}

  FlagpoleSitta::CommonFs.flagpole_cache_write(flag_key, flag)

  flag

end

#mid_key_gen(route_id) ⇒ Object

Determines if its for an index array or show array.



24
25
26
27
28
29
30
# File 'lib/flagpole_sitta/cache_sitta.rb', line 24

def mid_key_gen route_id
  if route_id
    mid_key = "#{route_id}/ShowArray"
  else
    mid_key = "IndexArray"
  end
end

#update_array_cache(key, options = {}) ⇒ Object

Updates the ‘array’ in the cache. Options :route_id which determines the type of mid_key



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/flagpole_sitta/cache_sitta.rb', line 63

def update_array_cache key, options={}

  flag_key = array_cache_key_gen "Flag", options[:route_id]

  flag = FlagpoleSitta::CommonFs.flagpole_cache_read(flag_key)

  #AR - If it doesn't exist start the process of creating it
  if flag.nil?
    flag = initialize_array_cache options[:route_id]
  end

  if flag[:empty] > -1
    #Find any empty container to use by popping it off of the top of the "stack".
    empty_key = array_cache_key_gen flag[:empty], options[:route_id], :emptystack => true

    i = FlagpoleSitta::CommonFs.flagpole_cache_read(empty_key)
    #Sense its going to be used remove its reference from the Stack.
    FlagpoleSitta::CommonFs.flagpole_cache_delete(empty_key)
    #Update the empty on flag to now hit the newest none used container on the stack.
    flag[:empty] = flag[:empty] - 1
  else
    #AR - update the array's end point
    flag[:space] = flag[:space] + 1
    i = flag[:space]
  end
 
  #AR - write out the new index at the end of the array
  array_key = array_cache_key_gen i, options[:route_id]
  FlagpoleSitta::CommonFs.flagpole_cache_write(array_key, {:key => key, :scope => options[:scope]})

  #AR - update flag in the cache
  flag_key = array_cache_key_gen "Flag", options[:route_id]
  FlagpoleSitta::CommonFs.flagpole_cache_write(flag_key, flag)

  array_key

end