package  session 
 
import  ( 
	"context"  
	"io"  
	"os"  
	"sync"  
 
	"github.com/go-faster/errors"  
) 
 
 
 
type  StorageMemory  struct  { 
	mux   sync .RWMutex  
	data  []byte  
} 
 
 
 
func  (s  *StorageMemory ) Dump  (w  io .Writer ) error  { 
	if  s  == nil  { 
		return  ErrNotFound  
	} 
	s .mux .RLock () 
	defer  s .mux .RUnlock () 
 
	if  len (s .data ) == 0  { 
		return  ErrNotFound  
	} 
 
	if  _ , err  := w .Write (s .data ); err  != nil  { 
		return  errors .Wrap (err , "write session" ) 
	} 
 
	return  nil  
} 
 
 
 
func  (s  *StorageMemory ) WriteFile  (name  string , perm  os .FileMode ) error  { 
	data , err  := s .Bytes (nil ) 
	if  err  != nil  { 
		return  err  
	} 
	return  os .WriteFile (name , data , perm ) 
} 
 
 
 
func  (s  *StorageMemory ) Bytes  (to  []byte ) ([]byte , error ) { 
	if  s  == nil  { 
		return  nil , ErrNotFound  
	} 
	s .mux .RLock () 
	defer  s .mux .RUnlock () 
 
	if  len (s .data ) == 0  { 
		return  nil , ErrNotFound  
	} 
 
	return  append (to , s .data ...), nil  
} 
 
 
func  (s  *StorageMemory ) Clone  () *StorageMemory  { 
	s2  := &StorageMemory {} 
 
	s2 .data , _ = s .Bytes (s2 .data ) 
	return  s2  
} 
 
 
func  (s  *StorageMemory ) LoadSession  (context .Context ) ([]byte , error ) { 
	if  s  == nil  { 
		return  nil , ErrNotFound  
	} 
	s .mux .RLock () 
	defer  s .mux .RUnlock () 
 
	if  len (s .data ) == 0  { 
		return  nil , ErrNotFound  
	} 
	cpy  := append ([]byte (nil ), s .data ...) 
 
	return  cpy , nil  
} 
 
 
func  (s  *StorageMemory ) StoreSession  (ctx  context .Context , data  []byte ) error  { 
	if  s  == nil  { 
		return  errors .New ("StoreSession called on StorageMemory(nil)" ) 
	} 
 
	s .mux .Lock () 
	s .data  = data  
	s .mux .Unlock () 
	return  nil  
} 
  
The pages are generated with Golds   v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds  is a Go 101  project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1  (reachable from the left QR code) to get the latest news of Golds .