Repository navigation
Expand file tree
/
Copy pathWCS_BrainCache.lua
More file actions
368 lines (309 loc) · 11.3 KB
/
Copy pathWCS_BrainCache.lua
File metadata and controls
368 lines (309 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
--[[
WCS_BrainCache.lua - Sistema de Cache y Optimización v6.6.0
Compatible con Lua 5.0 (WoW 1.12 / Turtle WoW)
Sistema de cache para optimizar llamadas costosas
Autor: Elnazzareno (DarckRovert)
Twitch: twitch.tv/darckrovert
Kick: kick.com/darckrovert
]]--
if WCS_Brain and WCS_Brain.ENABLED == false then return end
WCS_BrainCache = WCS_BrainCache or {}
WCS_BrainCache.VERSION = "6.6.0"
-- ============================================================================
-- CONFIGURACIÓN
-- ============================================================================
WCS_BrainCache.Config = {
enabled = true,
defaultTTL = 0.5, -- 500ms por defecto
maxCacheSize = 100,
autoCleanup = true,
cleanupInterval = 60 -- Limpiar cada 60 segundos
}
-- ============================================================================
-- ALMACENAMIENTO DE CACHE
-- ============================================================================
WCS_BrainCache.Storage = {}
WCS_BrainCache.Stats = {
hits = 0,
misses = 0,
evictions = 0
}
-- ============================================================================
-- INICIALIZACIÓN
-- ============================================================================
function WCS_BrainCache:Initialize()
self.Storage = {}
self.Stats = { hits = 0, misses = 0, evictions = 0 }
-- Iniciar limpieza automática
if self.Config.autoCleanup then
self:StartAutoCleanup()
end
if WCS_BrainLogger then
WCS_BrainLogger:Info("Cache", "Sistema de cache inicializado")
end
end
-- ============================================================================
-- OPERACIONES DE CACHE
-- ============================================================================
function WCS_BrainCache:Get(key)
if not self.Config.enabled then
return nil
end
local entry = self.Storage[key]
if not entry then
self.Stats.misses = self.Stats.misses + 1
return nil
end
-- Verificar TTL
local now = GetTime()
if entry.expiry and now > entry.expiry then
-- Cache expirado
self.Storage[key] = nil
self.Stats.misses = self.Stats.misses + 1
return nil
end
-- Cache hit
self.Stats.hits = self.Stats.hits + 1
entry.lastAccess = now
return entry.data
end
function WCS_BrainCache:Set(key, data, ttl)
if not self.Config.enabled then
return
end
ttl = ttl or self.Config.defaultTTL
local now = GetTime()
self.Storage[key] = {
data = data,
timestamp = now,
expiry = ttl and (now + ttl) or nil,
lastAccess = now
}
-- Verificar tamaño del cache
self:CheckCacheSize()
end
function WCS_BrainCache:Has(key)
return self:Get(key) ~= nil
end
function WCS_BrainCache:Delete(key)
if self.Storage[key] then
self.Storage[key] = nil
return true
end
return false
end
function WCS_BrainCache:Clear()
self.Storage = {}
if WCS_BrainLogger then
WCS_BrainLogger:Info("Cache", "Cache limpiado")
end
end
-- ============================================================================
-- GESTIÓN DE TAMAÑO
-- ============================================================================
function WCS_BrainCache:CheckCacheSize()
local count = 0
for _ in pairs(self.Storage) do
count = count + 1
end
if count > self.Config.maxCacheSize then
self:EvictOldest()
end
end
function WCS_BrainCache:EvictOldest()
local oldest = nil
local oldestTime = nil
for key, entry in pairs(self.Storage) do
if not oldestTime or entry.lastAccess < oldestTime then
oldest = key
oldestTime = entry.lastAccess
end
end
if oldest then
self.Storage[oldest] = nil
self.Stats.evictions = self.Stats.evictions + 1
end
end
-- ============================================================================
-- LIMPIEZA AUTOMÁTICA
-- ============================================================================
function WCS_BrainCache:StartAutoCleanup()
if not self.cleanupFrame then
self.cleanupFrame = CreateFrame("Frame")
self.cleanupFrame.elapsed = 0
self.cleanupFrame:SetScript("OnUpdate", function()
this.elapsed = this.elapsed + arg1
if this.elapsed >= WCS_BrainCache.Config.cleanupInterval then
this.elapsed = 0
WCS_BrainCache:CleanupExpired()
end
end)
end
end
function WCS_BrainCache:CleanupExpired()
local now = GetTime()
local cleaned = 0
for key, entry in pairs(self.Storage) do
if entry.expiry and now > entry.expiry then
self.Storage[key] = nil
cleaned = cleaned + 1
end
end
if cleaned > 0 and WCS_BrainLogger then
WCS_BrainLogger:Debug("Cache", "Limpiadas " .. cleaned .. " entradas expiradas")
end
end
-- ============================================================================
-- ESTADÍSTICAS
-- ============================================================================
function WCS_BrainCache:GetStats()
local total = self.Stats.hits + self.Stats.misses
local hitRate = total > 0 and (self.Stats.hits / total * 100) or 0
return {
hits = self.Stats.hits,
misses = self.Stats.misses,
evictions = self.Stats.evictions,
hitRate = hitRate,
size = self:GetSize()
}
end
function WCS_BrainCache:GetSize()
local count = 0
for _ in pairs(self.Storage) do
count = count + 1
end
return count
end
function WCS_BrainCache:ResetStats()
self.Stats = { hits = 0, misses = 0, evictions = 0 }
end
-- ============================================================================
-- FUNCIONES HELPER PARA CACHEAR FUNCIONES
-- ============================================================================
function WCS_BrainCache:Memoize(func, ttl)
return function(...)
local args = arg
local key = "memoize_" .. tostring(func)
for i = 1, table.getn(args) do
key = key .. "_" .. tostring(args[i])
end
local cached = self:Get(key)
if cached ~= nil then
return cached
end
local result = func(unpack(args))
self:Set(key, result, ttl)
return result
end
end
-- ============================================================================
-- CACHE ESPECÍFICO PARA WCS_BRAIN
-- ============================================================================
-- Cache de información del target
function WCS_BrainCache:GetTargetInfo()
local cached = self:Get("targetInfo")
if cached then return cached end
if not UnitExists("target") then
return nil
end
local info = {
exists = true,
name = UnitName("target"),
level = UnitLevel("target"),
health = UnitHealth("target"),
healthMax = UnitHealthMax("target"),
healthPct = (UnitHealth("target") / UnitHealthMax("target")) * 100,
isHostile = UnitCanAttack("player", "target"),
isDead = UnitIsDead("target"),
classification = UnitClassification("target"),
isPlayer = UnitIsPlayer("target"),
class = UnitClass("target")
}
self:Set("targetInfo", info, 0.1) -- Cache por 100ms
return info
end
-- Cache de información del jugador
function WCS_BrainCache:GetPlayerInfo()
local cached = self:Get("playerInfo")
if cached then return cached end
local info = {
health = UnitHealth("player"),
healthMax = UnitHealthMax("player"),
healthPct = (UnitHealth("player") / UnitHealthMax("player")) * 100,
mana = UnitMana("player"),
manaMax = UnitManaMax("player"),
manaPct = (UnitMana("player") / UnitManaMax("player")) * 100,
inCombat = UnitAffectingCombat("player"),
isDead = UnitIsDead("player")
}
self:Set("playerInfo", info, 0.1) -- Cache por 100ms
return info
end
-- Cache de información de la mascota
function WCS_BrainCache:GetPetInfo()
local cached = self:Get("petInfo")
if cached then return cached end
if not UnitExists("pet") then
return nil
end
local info = {
exists = true,
name = UnitName("pet"),
health = UnitHealth("pet"),
healthMax = UnitHealthMax("pet"),
healthPct = (UnitHealth("pet") / UnitHealthMax("pet")) * 100,
mana = UnitMana("pet"),
manaMax = UnitManaMax("pet"),
manaPct = UnitManaMax("pet") > 0 and (UnitMana("pet") / UnitManaMax("pet")) * 100 or 0,
isDead = UnitIsDead("pet")
}
self:Set("petInfo", info, 0.1) -- Cache por 100ms
return info
end
-- Cache de costos de hechizos
function WCS_BrainCache:GetSpellCost(spellName)
-- NOTA: GetSpellInfo no existe en WoW 1.12
-- No hay forma directa de obtener el costo de mana desde la API
-- Retornar 0 por ahora (se puede implementar una tabla manual si es necesario)
return 0
end
-- ============================================================================
-- COMANDOS SLASH
-- ============================================================================
SLASH_WCSCACHE1 = "/wcscache"
SLASH_WCSCACHE2 = "/braincache"
SlashCmdList["WCSCACHE"] = function(msg)
if msg == "stats" then
local stats = WCS_BrainCache:GetStats()
DEFAULT_CHAT_FRAME:AddMessage("|cFF9482C9[WCS Cache]|r Estadísticas:")
DEFAULT_CHAT_FRAME:AddMessage(" Hits: " .. stats.hits)
DEFAULT_CHAT_FRAME:AddMessage(" Misses: " .. stats.misses)
DEFAULT_CHAT_FRAME:AddMessage(" Hit Rate: " .. string.format("%.1f%%", stats.hitRate))
DEFAULT_CHAT_FRAME:AddMessage(" Evictions: " .. stats.evictions)
DEFAULT_CHAT_FRAME:AddMessage(" Size: " .. stats.size .. "/" .. WCS_BrainCache.Config.maxCacheSize)
elseif msg == "clear" then
WCS_BrainCache:Clear()
DEFAULT_CHAT_FRAME:AddMessage("|cFF9482C9[WCS Cache]|r Cache limpiado")
elseif msg == "reset" then
WCS_BrainCache:ResetStats()
DEFAULT_CHAT_FRAME:AddMessage("|cFF9482C9[WCS Cache]|r Estadísticas reseteadas")
else
DEFAULT_CHAT_FRAME:AddMessage("|cFF9482C9[WCS Cache]|r Comandos:")
DEFAULT_CHAT_FRAME:AddMessage("|cFFFFCC00/braincache stats|r - Ver estadísticas")
DEFAULT_CHAT_FRAME:AddMessage("|cFFFFCC00/braincache clear|r - Limpiar cache")
DEFAULT_CHAT_FRAME:AddMessage("|cFFFFCC00/braincache reset|r - Resetear estadísticas")
end
end
-- ============================================================================
-- AUTO-INICIALIZACIÓN
-- ============================================================================
local function OnLoad()
WCS_BrainCache:Initialize()
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function()
if event == "ADDON_LOADED" and arg1 == "WCS_Brain" then
OnLoad()
end
end)