File size: 11,049 Bytes
25f22bf |
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
import localforage from 'localforage';
class CacheService {
constructor() {
this.storage = localforage.createInstance({
name: 'LinAppCache',
storeName: 'authCache'
});
this.cachePrefix = 'lin_cache_';
this.defaultTTL = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds
}
/**
* Generate a cache key with prefix
* @param {string} key - The cache key
* @returns {string} - Full cache key
*/
_generateKey(key) {
return `${this.cachePrefix}${key}`;
}
/**
* Set data in cache with TTL
* @param {string} key - Cache key
* @param {any} data - Data to cache
* @param {number} ttl - Time to live in milliseconds (optional)
* @returns {Promise<void>}
*/
async set(key, data, ttl = this.defaultTTL) {
const cacheKey = this._generateKey(key);
const expiry = Date.now() + ttl;
const cacheData = {
data,
expiry,
metadata: {
createdAt: Date.now(),
ttl,
key
}
};
try {
await this.storage.setItem(cacheKey, cacheData);
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log(`ποΈ [Cache] Set cache for key: ${key}`);
}
} catch (error) {
console.error(`ποΈ [Cache] Error setting cache for key: ${key}`, error);
throw error;
}
}
/**
* Get data from cache
* @param {string} key - Cache key
* @returns {Promise<any|null>} - Cached data or null if not found/expired
*/
async get(key) {
const cacheKey = this._generateKey(key);
try {
const cacheData = await this.storage.getItem(cacheKey);
if (!cacheData) {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log(`ποΈ [Cache] Cache miss for key: ${key}`);
}
return null;
}
// Check if cache has expired
if (Date.now() > cacheData.expiry) {
await this.remove(key);
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log(`ποΈ [Cache] Cache expired for key: ${key}`);
}
return null;
}
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log(`ποΈ [Cache] Cache hit for key: ${key}`);
}
return cacheData.data;
} catch (error) {
console.error(`ποΈ [Cache] Error getting cache for key: ${key}`, error);
return null;
}
}
/**
* Remove data from cache
* @param {string} key - Cache key
* @returns {Promise<void>}
*/
async remove(key) {
const cacheKey = this._generateKey(key);
try {
await this.storage.removeItem(cacheKey);
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log(`ποΈ [Cache] Removed cache for key: ${key}`);
}
} catch (error) {
console.error(`ποΈ [Cache] Error removing cache for key: ${key}`, error);
throw error;
}
}
/**
* Clear all cached data
* @returns {Promise<void>}
*/
async clear() {
try {
await this.storage.clear();
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('ποΈ [Cache] Cleared all cache');
}
} catch (error) {
console.error('ποΈ [Cache] Error clearing cache', error);
throw error;
}
}
/**
* Check if cache exists and is valid
* @param {string} key - Cache key
* @returns {Promise<boolean>} - True if cache exists and is valid
*/
async exists(key) {
const cacheKey = this._generateKey(key);
try {
const cacheData = await this.storage.getItem(cacheKey);
if (!cacheData) {
return false;
}
// Check if cache has expired
if (Date.now() > cacheData.expiry) {
await this.remove(key);
return false;
}
return true;
} catch (error) {
console.error(`ποΈ [Cache] Error checking cache existence for key: ${key}`, error);
return false;
}
}
/**
* Get cache metadata
* @param {string} key - Cache key
* @returns {Promise<Object|null>} - Cache metadata or null
*/
async getMetadata(key) {
const cacheKey = this._generateKey(key);
try {
const cacheData = await this.storage.getItem(cacheKey);
if (!cacheData) {
return null;
}
return cacheData.metadata || null;
} catch (error) {
console.error(`ποΈ [Cache] Error getting cache metadata for key: ${key}`, error);
return null;
}
}
/**
* Set user authentication cache
* @param {Object} authData - Authentication data
* @param {boolean} rememberMe - Remember me flag
* @returns {Promise<void>}
*/
async setAuthCache(authData, rememberMe = false) {
const ttl = rememberMe ? this.defaultTTL : 60 * 60 * 1000; // 1 hour for normal session
await this.set('auth_token', authData.token, ttl);
await this.set('user_data', authData.user, ttl);
await this.set('remember_me', rememberMe, ttl);
await this.set('auth_expiry', Date.now() + ttl, ttl);
// Store device fingerprint for security
const deviceFingerprint = this.generateDeviceFingerprint();
await this.set('device_fingerprint', deviceFingerprint, ttl);
}
/**
* Get user authentication cache
* @returns {Promise<Object|null>} - Authentication cache data or null
*/
async getAuthCache() {
try {
const [token, userData, rememberMe, expiry, deviceFingerprint] = await Promise.all([
this.get('auth_token'),
this.get('user_data'),
this.get('remember_me'),
this.get('auth_expiry'),
this.get('device_fingerprint')
]);
if (!token || !userData || !expiry) {
return null;
}
// Check if cache has expired
if (Date.now() > expiry) {
await this.clearAuthCache();
return null;
}
// Validate device fingerprint for security
const currentFingerprint = this.generateDeviceFingerprint();
if (deviceFingerprint && deviceFingerprint !== currentFingerprint) {
await this.clearAuthCache();
return null;
}
return {
token,
user: userData,
rememberMe: rememberMe || false,
expiresAt: expiry,
deviceFingerprint
};
} catch (error) {
console.error('ποΈ [Cache] Error getting auth cache', error);
return null;
}
}
/**
* Clear authentication cache
* @returns {Promise<void>}
*/
async clearAuthCache() {
const keys = ['auth_token', 'user_data', 'remember_me', 'auth_expiry', 'device_fingerprint'];
await Promise.all(keys.map(key => this.remove(key)));
// Also clear localStorage for consistency
localStorage.removeItem('token');
localStorage.removeItem('rememberMePreference');
}
/**
* Generate device fingerprint for security
* @returns {string} - Device fingerprint
*/
generateDeviceFingerprint() {
const userAgent = navigator.userAgent;
const screenResolution = `${screen.width}x${screen.height}`;
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const language = navigator.language;
// Create a simple hash-like string
const fingerprint = `${userAgent}-${screenResolution}-${timezone}-${language}`;
return btoa(fingerprint).replace(/[^a-zA-Z0-9]/g, '').substring(0, 32);
}
/**
* Cache user preferences
* @param {Object} preferences - User preferences
* @returns {Promise<void>}
*/
async setUserPreferences(preferences) {
await this.set('user_preferences', preferences, 30 * 24 * 60 * 60 * 1000); // 30 days
}
/**
* Get user preferences
* @returns {Promise<Object|null>} - User preferences or null
*/
async getUserPreferences() {
return await this.get('user_preferences');
}
/**
* Cache API responses to reduce network requests
* @param {string} endpoint - API endpoint
* @param {Object} data - Response data
* @param {number} ttl - Cache TTL in milliseconds
* @returns {Promise<void>}
*/
async cacheApiResponse(endpoint, data, ttl = 5 * 60 * 1000) { // 5 minutes default
await this.set(`api_${endpoint}`, data, ttl);
}
/**
* Get cached API response
* @param {string} endpoint - API endpoint
* @returns {Promise<Object|null>} - Cached response or null
*/
async getCachedApiResponse(endpoint) {
return await this.get(`api_${endpoint}`);
}
/**
* Set authentication token
* @param {string} token - JWT token
* @param {boolean} rememberMe - Remember me flag
* @returns {Promise<void>}
*/
async setAuthToken(token, rememberMe = false) {
const ttl = rememberMe ? this.defaultTTL : 60 * 60 * 1000; // 1 hour for normal session
await this.set('auth_token', token, ttl);
await this.set('remember_me', rememberMe, ttl);
await this.set('auth_expiry', Date.now() + ttl, ttl);
}
/**
* Get authentication token
* @returns {Promise<Object|null>} - Auth token data or null
*/
async getAuthToken() {
try {
const [token, rememberMe, expiry] = await Promise.all([
this.get('auth_token'),
this.get('remember_me'),
this.get('auth_expiry')
]);
if (!token || !expiry) {
return null;
}
// Check if cache has expired
if (Date.now() > expiry) {
await this.clearAuthToken();
return null;
}
return {
token,
rememberMe: rememberMe || false,
expiresAt: expiry
};
} catch (error) {
console.error('ποΈ [Cache] Error getting auth token', error);
return null;
}
}
/**
* Clear authentication token
* @returns {Promise<void>}
*/
async clearAuthToken() {
const keys = ['auth_token', 'remember_me', 'auth_expiry'];
await Promise.all(keys.map(key => this.remove(key)));
localStorage.removeItem('token');
localStorage.removeItem('rememberMePreference');
}
/**
* Set user data
* @param {Object} userData - User data object
* @param {boolean} rememberMe - Remember me flag
* @returns {Promise<void>}
*/
async setUserData(userData, rememberMe = false) {
const ttl = rememberMe ? this.defaultTTL : 60 * 60 * 1000; // 1 hour for normal session
await this.set('user_data', userData, ttl);
}
/**
* Get user data
* @returns {Promise<Object|null>} - User data or null
*/
async getUserData() {
return await this.get('user_data');
}
/**
* Set session information
* @param {Object} sessionData - Session data
* @returns {Promise<void>}
*/
async setSessionInfo(sessionData) {
const ttl = sessionData.rememberMe ? this.defaultTTL : 60 * 60 * 1000; // 1 hour for normal session
await this.set('session_info', sessionData, ttl);
}
/**
* Get session information
* @returns {Promise<Object|null>} - Session data or null
*/
async getSessionInfo() {
return await this.get('session_info');
}
}
// Export singleton instance
export default new CacheService(); |