mirror of
				https://github.com/crater-invoice/crater.git
				synced 2025-11-04 06:23:17 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			35 lines
		
	
	
		
			639 B
		
	
	
	
		
			JavaScript
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
		
			639 B
		
	
	
	
		
			JavaScript
		
	
	
		
			Executable File
		
	
	
	
	
class ExpiringStorage {
 | 
						|
  get (key) {
 | 
						|
    const cached = JSON.parse(
 | 
						|
      localStorage.getItem(key)
 | 
						|
    )
 | 
						|
 | 
						|
    if (!cached) {
 | 
						|
      return null
 | 
						|
    }
 | 
						|
 | 
						|
    const expires = new Date(cached.expires)
 | 
						|
 | 
						|
    if (expires < new Date()) {
 | 
						|
      localStorage.removeItem(key)
 | 
						|
      return null
 | 
						|
    }
 | 
						|
 | 
						|
    return cached.value
 | 
						|
  }
 | 
						|
 | 
						|
  has (key) {
 | 
						|
    return this.get(key) !== null
 | 
						|
  }
 | 
						|
 | 
						|
  set (key, value, lifeTimeInMinutes) {
 | 
						|
    const currentTime = new Date().getTime()
 | 
						|
 | 
						|
    const expires = new Date(currentTime + lifeTimeInMinutes * 60000)
 | 
						|
 | 
						|
    localStorage.setItem(key, JSON.stringify({ value, expires }))
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
export default new ExpiringStorage()
 |