Documentation
¶
Overview ¶
Package filecopy provides a high-performance file copying service with persistent caching. It creates temporary copies of files that can be reused across application restarts, significantly reducing I/O overhead for large files.
Key features:
- Instance-based isolation: Different instance IDs maintain separate cache namespaces
- Persistent caching: Temporary files survive application restarts
- Automatic cleanup: Removes orphaned files and manages cache lifecycle
- Thread-safe operations: Concurrent access is fully supported
- Version management: Only keeps the latest version of each cached file
Index ¶
Constants ¶
const ( // CleanupInterval defines how often to run unified cleanup (1 minute). // First run is delayed by CleanupInterval after manager initialization. CleanupInterval = 1 * time.Minute // OrphanFileCleanupThreshold defines when orphaned files should be cleaned up (10 minutes). OrphanFileCleanupThreshold = 10 * time.Minute // MaxCacheEntries defines the maximum number of files to keep in the cache to prevent memory leaks. MaxCacheEntries = 10000 // Reasonable limit for most use cases // RecentFileProtectionWindow prevents deletion of recently modified/accessed files. RecentFileProtectionWindow = 2 * CleanupInterval // DedupSkipWindow skips version deduplication for very recent files during periodic cleanup. DedupSkipWindow = CleanupInterval // PathHashHexLen limits path-hash length in filenames (increase to lower collision risk). PathHashHexLen = 12 // DataHashHexLen limits data-hash length in filenames. DataHashHexLen = 16 // MaxBaseNameLen limits base filename length in temp file naming. MaxBaseNameLen = 100 )
Configuration constants for cache management and behavior tuning.
const VersionDetection = VersionDetectContentHash
VersionDetection controls how data hash is computed. Adjust as needed.
Variables ¶
This section is empty.
Functions ¶
func GetTempCopy ¶
GetTempCopy creates or retrieves a temporary copy of the specified file. It provides persistent caching with instance-based isolation.
Parameters:
- instanceID: Unique identifier for the application instance (e.g., "app_v1.0", "service_name")
- originalPath: Absolute path to the original file to copy
Returns:
- string: Path to the temporary copy
- error: Any error encountered during the operation
The function performs these operations:
- Checks in-memory cache for existing valid copy
- Scans disk for existing cached file that can be reused
- Creates new copy if none found, cleaning up old versions
Thread-safe for concurrent use.
Types ¶
type FileCopyManager ¶
type FileCopyManager struct {
// contains filtered or unexported fields
}
FileCopyManager manages temporary file copies with persistent caching capabilities. It provides thread-safe operations for creating, accessing, and cleaning up temporary files.
func (*FileCopyManager) GetTempCopy ¶
func (fm *FileCopyManager) GetTempCopy(originalPath string) (string, error)
GetTempCopy implements optimized file copying with intelligent index-based lookup. This eliminates repeated directory scanning and provides O(1) lookup performance. Old file versions are cleaned up by the periodic cleanup worker, not here.
func (*FileCopyManager) Shutdown ¶
func (fm *FileCopyManager) Shutdown()
Shutdown performs complete cleanup by removing all temporary files and cache entries. This method ensures clean resource deallocation with proper goroutine lifecycle management.
type FileIndexEntry ¶
type FileIndexEntry struct {
TempPath string // Path to the temporary file copy (immutable after creation)
OriginalPath string // Original source file path (protected by mu)
Size int64 // Size of the original file in bytes (immutable after creation)
ModTime time.Time // Modification time of the original file (immutable after creation)
PathHash string // Path hash for collision detection (immutable after creation)
DataHash string // Content hash for file integrity verification (immutable after creation)
BaseName string // Base name for multi-version cleanup (immutable after creation)
Extension string // File extension (normalized, without leading dot) (immutable after creation)
// contains filtered or unexported fields
}
FileIndexEntry represents an indexed temporary file with comprehensive metadata. It provides O(1) lookup and intelligent file lifecycle management. Thread-safe for concurrent access through atomic operations and mutex protection.
func (*FileIndexEntry) GetLastAccess ¶
func (e *FileIndexEntry) GetLastAccess() time.Time
GetLastAccess returns the last access time in a thread-safe manner
func (*FileIndexEntry) GetOriginalPath ¶
func (e *FileIndexEntry) GetOriginalPath() string
GetOriginalPath returns the original path in a thread-safe manner
func (*FileIndexEntry) SetLastAccess ¶
func (e *FileIndexEntry) SetLastAccess(t time.Time)
SetLastAccess updates the last access time atomically
func (*FileIndexEntry) SetOriginalPath ¶
func (e *FileIndexEntry) SetOriginalPath(path string)
SetOriginalPath updates the original path in a thread-safe manner
type VersionDetectionMode ¶
type VersionDetectionMode int
Version detection policy for generating data hash in file naming and cache keys.
const ( // VersionDetectContentHash computes data hash from entire file content (strong consistency). VersionDetectContentHash VersionDetectionMode = iota // VersionDetectSizeModTime computes data hash from size+modtime only (faster, weaker consistency). VersionDetectSizeModTime )