File size: 8,713 Bytes
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * ็ผ“ๅญ˜็›‘ๆŽงๅ’Œ็ฎก็†ๅทฅๅ…ท
 * ๆไพ›็ปŸไธ€็š„็ผ“ๅญ˜็›‘ๆŽงใ€็ปŸ่ฎกๅ’Œๅฎ‰ๅ…จๆธ…็†ๅŠŸ่ƒฝ
 */

const logger = require('./logger')
const crypto = require('crypto')

class CacheMonitor {
  constructor() {
    this.monitors = new Map() // ๅญ˜ๅ‚จๆ‰€ๆœ‰่ขซ็›‘ๆŽง็š„็ผ“ๅญ˜ๅฎžไพ‹
    this.startTime = Date.now()
    this.totalHits = 0
    this.totalMisses = 0
    this.totalEvictions = 0

    // ๐Ÿ”’ ๅฎ‰ๅ…จ้…็ฝฎ
    this.securityConfig = {
      maxCacheAge: 15 * 60 * 1000, // ๆœ€ๅคง็ผ“ๅญ˜ๅนด้พ„ 15 ๅˆ†้’Ÿ
      forceCleanupInterval: 30 * 60 * 1000, // ๅผบๅˆถๆธ…็†้—ด้š” 30 ๅˆ†้’Ÿ
      memoryThreshold: 100 * 1024 * 1024, // ๅ†…ๅญ˜้˜ˆๅ€ผ 100MB
      sensitiveDataPatterns: [/password/i, /token/i, /secret/i, /key/i, /credential/i]
    }

    // ๐Ÿงน ๅฎšๆœŸๆ‰ง่กŒๅฎ‰ๅ…จๆธ…็†
    this.setupSecurityCleanup()

    // ๐Ÿ“Š ๅฎšๆœŸๆŠฅๅ‘Š็ปŸ่ฎกไฟกๆฏ
    this.setupPeriodicReporting()
  }

  /**
   * ๆณจๅ†Œ็ผ“ๅญ˜ๅฎžไพ‹่ฟ›่กŒ็›‘ๆŽง
   * @param {string} name - ็ผ“ๅญ˜ๅ็งฐ
   * @param {LRUCache} cache - ็ผ“ๅญ˜ๅฎžไพ‹
   */
  registerCache(name, cache) {
    if (this.monitors.has(name)) {
      logger.warn(`โš ๏ธ Cache ${name} is already registered, updating reference`)
    }

    this.monitors.set(name, {
      cache,
      registeredAt: Date.now(),
      lastCleanup: Date.now(),
      totalCleanups: 0
    })

    logger.info(`๐Ÿ“ฆ Registered cache for monitoring: ${name}`)
  }

  /**
   * ่Žทๅ–ๆ‰€ๆœ‰็ผ“ๅญ˜็š„็ปผๅˆ็ปŸ่ฎก
   */
  getGlobalStats() {
    const stats = {
      uptime: Math.floor((Date.now() - this.startTime) / 1000), // ็ง’
      cacheCount: this.monitors.size,
      totalSize: 0,
      totalHits: 0,
      totalMisses: 0,
      totalEvictions: 0,
      averageHitRate: 0,
      caches: {}
    }

    for (const [name, monitor] of this.monitors) {
      const cacheStats = monitor.cache.getStats()
      stats.totalSize += cacheStats.size
      stats.totalHits += cacheStats.hits
      stats.totalMisses += cacheStats.misses
      stats.totalEvictions += cacheStats.evictions

      stats.caches[name] = {
        ...cacheStats,
        lastCleanup: new Date(monitor.lastCleanup).toISOString(),
        totalCleanups: monitor.totalCleanups,
        age: Math.floor((Date.now() - monitor.registeredAt) / 1000) // ็ง’
      }
    }

    const totalRequests = stats.totalHits + stats.totalMisses
    stats.averageHitRate =
      totalRequests > 0 ? `${((stats.totalHits / totalRequests) * 100).toFixed(2)}%` : '0%'

    return stats
  }

  /**
   * ๐Ÿ”’ ๆ‰ง่กŒๅฎ‰ๅ…จๆธ…็†
   * ๆธ…็†่ฟ‡ๆœŸๆ•ฐๆฎๅ’Œๆฝœๅœจ็š„ๆ•ๆ„Ÿไฟกๆฏ
   */
  performSecurityCleanup() {
    logger.info('๐Ÿ”’ Starting security cleanup for all caches')

    for (const [name, monitor] of this.monitors) {
      try {
        const { cache } = monitor
        const beforeSize = cache.cache.size

        // ๆ‰ง่กŒๅธธ่ง„ๆธ…็†
        cache.cleanup()

        // ๆฃ€ๆŸฅ็ผ“ๅญ˜ๅนด้พ„๏ผŒๅฆ‚ๆžœๅคช่€ๅˆ™ๅฎŒๅ…จๆธ…็ฉบ
        const cacheAge = Date.now() - monitor.registeredAt
        if (cacheAge > this.securityConfig.maxCacheAge * 2) {
          logger.warn(
            `โš ๏ธ Cache ${name} is too old (${Math.floor(cacheAge / 60000)}min), performing full clear`
          )
          cache.clear()
        }

        monitor.lastCleanup = Date.now()
        monitor.totalCleanups++

        const afterSize = cache.cache.size
        if (beforeSize !== afterSize) {
          logger.info(`๐Ÿงน Cache ${name}: Cleaned ${beforeSize - afterSize} items`)
        }
      } catch (error) {
        logger.error(`โŒ Error cleaning cache ${name}:`, error)
      }
    }
  }

  /**
   * ๐Ÿ“Š ็”Ÿๆˆ่ฏฆ็ป†ๆŠฅๅ‘Š
   */
  generateReport() {
    const stats = this.getGlobalStats()

    logger.info('โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•')
    logger.info('๐Ÿ“Š Cache System Performance Report')
    logger.info('โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•')
    logger.info(`โฑ๏ธ  Uptime: ${this.formatUptime(stats.uptime)}`)
    logger.info(`๐Ÿ“ฆ Active Caches: ${stats.cacheCount}`)
    logger.info(`๐Ÿ“ˆ Total Cache Size: ${stats.totalSize} items`)
    logger.info(`๐ŸŽฏ Global Hit Rate: ${stats.averageHitRate}`)
    logger.info(`โœ… Total Hits: ${stats.totalHits.toLocaleString()}`)
    logger.info(`โŒ Total Misses: ${stats.totalMisses.toLocaleString()}`)
    logger.info(`๐Ÿ—‘๏ธ  Total Evictions: ${stats.totalEvictions.toLocaleString()}`)
    logger.info('โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€')

    // ่ฏฆ็ป†็š„ๆฏไธช็ผ“ๅญ˜็ปŸ่ฎก
    for (const [name, cacheStats] of Object.entries(stats.caches)) {
      logger.info(`\n๐Ÿ“ฆ ${name}:`)
      logger.info(
        `   Size: ${cacheStats.size}/${cacheStats.maxSize} | Hit Rate: ${cacheStats.hitRate}`
      )
      logger.info(
        `   Hits: ${cacheStats.hits} | Misses: ${cacheStats.misses} | Evictions: ${cacheStats.evictions}`
      )
      logger.info(
        `   Age: ${this.formatUptime(cacheStats.age)} | Cleanups: ${cacheStats.totalCleanups}`
      )
    }
    logger.info('โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•')
  }

  /**
   * ๐Ÿงน ่ฎพ็ฝฎๅฎšๆœŸๅฎ‰ๅ…จๆธ…็†
   */
  setupSecurityCleanup() {
    // ๆฏ 10 ๅˆ†้’Ÿๆ‰ง่กŒไธ€ๆฌกๅฎ‰ๅ…จๆธ…็†
    setInterval(
      () => {
        this.performSecurityCleanup()
      },
      10 * 60 * 1000
    )

    // ๆฏ 30 ๅˆ†้’ŸๅผบๅˆถๅฎŒๆ•ดๆธ…็†
    setInterval(() => {
      logger.warn('โš ๏ธ Performing forced complete cleanup for security')
      for (const [name, monitor] of this.monitors) {
        monitor.cache.clear()
        logger.info(`๐Ÿ—‘๏ธ Force cleared cache: ${name}`)
      }
    }, this.securityConfig.forceCleanupInterval)
  }

  /**
   * ๐Ÿ“Š ่ฎพ็ฝฎๅฎšๆœŸๆŠฅๅ‘Š
   */
  setupPeriodicReporting() {
    // ๆฏ 5 ๅˆ†้’Ÿ็”Ÿๆˆไธ€ๆฌก็ฎ€ๅ•็ปŸ่ฎก
    setInterval(
      () => {
        const stats = this.getGlobalStats()
        logger.info(
          `๐Ÿ“Š Quick Stats - Caches: ${stats.cacheCount}, Size: ${stats.totalSize}, Hit Rate: ${stats.averageHitRate}`
        )
      },
      5 * 60 * 1000
    )

    // ๆฏ 30 ๅˆ†้’Ÿ็”Ÿๆˆไธ€ๆฌก่ฏฆ็ป†ๆŠฅๅ‘Š
    setInterval(
      () => {
        this.generateReport()
      },
      30 * 60 * 1000
    )
  }

  /**
   * ๆ ผๅผๅŒ–่ฟ่กŒๆ—ถ้—ด
   */
  formatUptime(seconds) {
    const hours = Math.floor(seconds / 3600)
    const minutes = Math.floor((seconds % 3600) / 60)
    const secs = seconds % 60

    if (hours > 0) {
      return `${hours}h ${minutes}m ${secs}s`
    } else if (minutes > 0) {
      return `${minutes}m ${secs}s`
    } else {
      return `${secs}s`
    }
  }

  /**
   * ๐Ÿ” ็”Ÿๆˆๅฎ‰ๅ…จ็š„็ผ“ๅญ˜้”ฎ
   * ไฝฟ็”จ SHA-256 ๅ“ˆๅธŒ้ฟๅ…ๆšด้œฒๅŽŸๅง‹ๆ•ฐๆฎ
   */
  static generateSecureCacheKey(data) {
    return crypto.createHash('sha256').update(data).digest('hex')
  }

  /**
   * ๐Ÿ›ก๏ธ ้ชŒ่ฏ็ผ“ๅญ˜ๆ•ฐๆฎๅฎ‰ๅ…จๆ€ง
   * ๆฃ€ๆŸฅๆ˜ฏๅฆๅŒ…ๅซๆ•ๆ„Ÿไฟกๆฏ
   */
  validateCacheSecurity(data) {
    const dataStr = typeof data === 'string' ? data : JSON.stringify(data)

    for (const pattern of this.securityConfig.sensitiveDataPatterns) {
      if (pattern.test(dataStr)) {
        logger.warn('โš ๏ธ Potential sensitive data detected in cache')
        return false
      }
    }

    return true
  }

  /**
   * ๐Ÿ’พ ่Žทๅ–ๅ†…ๅญ˜ไฝฟ็”จไผฐ็ฎ—
   */
  estimateMemoryUsage() {
    let totalBytes = 0

    for (const [, monitor] of this.monitors) {
      const { cache } = monitor.cache
      for (const [key, item] of cache) {
        // ็ฒ—็•ฅไผฐ็ฎ—๏ผškey ้•ฟๅบฆ + value ๅบๅˆ—ๅŒ–้•ฟๅบฆ
        totalBytes += key.length * 2 // UTF-16
        totalBytes += JSON.stringify(item).length * 2
      }
    }

    return {
      bytes: totalBytes,
      mb: (totalBytes / (1024 * 1024)).toFixed(2),
      warning: totalBytes > this.securityConfig.memoryThreshold
    }
  }

  /**
   * ๐Ÿšจ ็ดงๆ€ฅๆธ…็†
   * ๅœจๅ†…ๅญ˜ๅŽ‹ๅŠ›ๅคงๆ—ถไฝฟ็”จ
   */
  emergencyCleanup() {
    logger.error('๐Ÿšจ EMERGENCY CLEANUP INITIATED')

    for (const [name, monitor] of this.monitors) {
      const { cache } = monitor
      const beforeSize = cache.cache.size

      // ๆธ…็†ไธ€ๅŠ็š„็ผ“ๅญ˜้กน๏ผˆLRU ไผšไฟ็•™ๆœ€่ฟ‘ไฝฟ็”จ็š„๏ผ‰
      const targetSize = Math.floor(cache.maxSize / 2)
      while (cache.cache.size > targetSize) {
        const firstKey = cache.cache.keys().next().value
        cache.cache.delete(firstKey)
      }

      logger.warn(`๐Ÿšจ Emergency cleaned ${name}: ${beforeSize} -> ${cache.cache.size} items`)
    }
  }
}

// ๅฏผๅ‡บๅ•ไพ‹
module.exports = new CacheMonitor()