Helper Functions for Caching Documentation
This code provides helper functions and structures to manage various caches in the application, aiming to optimize performance by reducing database reads.
Constants
const (
SUMMARY_NO_SCHEDULE = "NoSchedule"
SUMMARY_NO_SHIFT = "NoShift"
SUMMARY_CANNOT_COMPUTE = "CannotCompute"
SUMMARY_INCOMPLETE = "Incomplete" // incomplete scans
SUMMARY_DAY_NO_WORK = "NoWork"
SUMMARY_DAY_WORK = "Work"
DEFAULT_DATE_RANGE = 30
DEFAULT_DAYS_IN_YEAR = 365
PRINT_DEBUG = true
)
SUMMARY_NO_SCHEDULE
,SUMMARY_NO_SHIFT
,SUMMARY_CANNOT_COMPUTE
,SUMMARY_INCOMPLETE
: Constants for summary statuses.SUMMARY_DAY_NO_WORK
,SUMMARY_DAY_WORK
: Constants for day statuses.DEFAULT_DATE_RANGE
: Default date range for caching.DEFAULT_DAYS_IN_YEAR
: Default number of days in a year.PRINT_DEBUG
: Flag to enable or disable debug printing.
AllCacheStruct
struct
type AllCacheStruct struct {
CompanyID string
Start string
End string
AutoShiftRule string
AppUser *AppUsers
Users *LocalUsers
Shifts *LocalShifts
Scans *LocalScanLog
Schedules *LocalSchedule
CalendarSchedules *LocalCalendarSchedule
Holidays *LocalCalendarHoliday
Leaves *LocalLeaves
Summaries *LocalSummaries
ScanRequests *LocalScanRequest
Settings *LocalSetting
}
CompanyID string
: The company ID for which data is cached.Start string
: The start date of the cached range.End string
: The end date of the cached range.AutoShiftRule string
: The auto shift rule.AppUser *AppUsers
: Cache for application users.Users *LocalUsers
: Cache for user profiles.Shifts *LocalShifts
: Cache for shifts.Scans *LocalScanLog
: Cache for scan logs.Schedules *LocalSchedule
: Cache for schedules.CalendarSchedules *LocalCalendarSchedule
: Cache for calendar schedules.Holidays *LocalCalendarHoliday
: Cache for holidays.Leaves *LocalLeaves
: Cache for leaves.Summaries *LocalSummaries
: Cache for summaries.ScanRequests *LocalScanRequest
: Cache for scan requests.Settings *LocalSetting
: Cache for settings.
Functions
CalendarSchedulesEmpty() bool
Checks if the calendar schedules cache is empty.
SummariesEmpty() bool
Checks if the summaries cache is empty.
IsCacheNeedRefresh(companyID, start, end string) bool
Checks if the cache needs to be refreshed based on the company ID and date range.
IsDateRangeInCache(start, end string) bool
Checks if a date range is within the cached range.
SetGlobalDao(pbx *core.BootstrapEvent) error
Sets the global DAO for all caches and reads data into the caches.
printlnDebug(a ...any)
Prints debug information if PRINT_DEBUG
is true.
Example Usage
package main
import (
"fio-backend/utils/caching"
"fio-backend/utils/timedate"
"github.com/pocketbase/pocketbase/core"
"time"
)
func main() {
// Simulate a PocketBase BootstrapEvent
pbx := &core.BootstrapEvent{
App: core.NewApp(),
}
// Set the global DAO and read data into caches
err := caching.SetGlobalDao(pbx)
if err != nil {
panic(err)
}
// Access the global cache
globalCache := caching.AllCacheStruct{
CompanyID: "your_company_id",
Start: timedate.TimeToString(time.Now().AddDate(0, -1, 0)), // 1 month ago
End: timedate.TimeToString(time.Now().AddDate(0, 1, 0)), // 1 month from now
}
// Check if the cache needs to be refreshed
if globalCache.IsCacheNeedRefresh("your_company_id", globalCache.Start, globalCache.End) {
// Refresh the cache
caching.SetGlobalDao(pbx)
}
// Use the cache
if !globalCache.CalendarSchedulesEmpty() {
// Process calendar schedules
}
if !globalCache.SummariesEmpty() {
// Process summaries
}
}
Improvements and Considerations
- Error Handling: Improve error handling in functions like
SetGlobalDao
by returning errors instead of just logging them. - Cache Invalidation: Implement a proper cache invalidation strategy (time-based or event-driven) to ensure data consistency.
- Singleton Pattern: Evaluate the use of the singleton pattern for global caches. Dependency injection offers better testability and flexibility.
- Code Clarity and Comments: Improve code comments to explain complex logic more clearly.
- Locking Granularity: Finer-grained locking (e.g., separate mutexes for different caches) could improve concurrency.
This comprehensive documentation provides a detailed understanding of the helper functions for caching and their functionalities. By addressing the suggested improvements, you can create a more robust and maintainable caching solution.