MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / saveStatsCache

Function saveStatsCache

source code/utils/statsCache.ts:216–256  ·  view source on GitHub ↗
(
  cache: PersistedStatsCache,
)

Source from the content-addressed store, hash-verified

214 * Uses a temp file + rename pattern to prevent corruption.
215 */
216export async function saveStatsCache(
217 cache: PersistedStatsCache,
218): Promise<void> {
219 const fs = getFsImplementation()
220 const cachePath = getStatsCachePath()
221 const tempPath = `${cachePath}.${randomBytes(8).toString('hex')}.tmp`
222
223 try {
224 // Ensure the directory exists
225 const configDir = getClaudeConfigHomeDir()
226 try {
227 await fs.mkdir(configDir)
228 } catch {
229 // Directory already exists or other error - proceed
230 }
231
232 // Write to temp file with fsync for atomic write safety
233 const content = jsonStringify(cache, null, 2)
234 const handle = await open(tempPath, 'w', 0o600)
235 try {
236 await handle.writeFile(content, { encoding: 'utf-8' })
237 await handle.sync()
238 } finally {
239 await handle.close()
240 }
241
242 // Atomic rename
243 await fs.rename(tempPath, cachePath)
244 logForDebugging(
245 `Stats cache saved successfully (lastComputedDate: ${cache.lastComputedDate})`,
246 )
247 } catch (error) {
248 logError(error)
249 // Clean up temp file
250 try {
251 await fs.unlink(tempPath)
252 } catch {
253 // Ignore cleanup errors
254 }
255 }
256}
257
258/**
259 * Merge new stats into an existing cache.

Callers 2

aggregateClaudeCodeStatsFunction · 0.85
loadStatsCacheFunction · 0.85

Calls 8

getFsImplementationFunction · 0.85
getStatsCachePathFunction · 0.85
jsonStringifyFunction · 0.85
openFunction · 0.85
logForDebuggingFunction · 0.85
logErrorFunction · 0.70
toStringMethod · 0.65
closeMethod · 0.45

Tested by

no test coverage detected