github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/cache.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package identity
    19  
    20  import (
    21  	"encoding/json"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  type cacheData struct {
    29  	Identity Identity `json:"identity"`
    30  }
    31  
    32  // IdentityCache saves identity to file
    33  type IdentityCache struct {
    34  	File string
    35  }
    36  
    37  // NewIdentityCache creates and returns identityCache
    38  func NewIdentityCache(dir string, jsonFile string) IdentityCacheInterface {
    39  	return &IdentityCache{
    40  		File: filepath.Join(dir, jsonFile),
    41  	}
    42  }
    43  
    44  // GetIdentity retrieves identity from cache
    45  func (ic *IdentityCache) GetIdentity() (identity Identity, err error) {
    46  	if !ic.cacheExists() {
    47  		err = errors.New("cache file does not exist")
    48  		return
    49  	}
    50  
    51  	cache, err := ic.readCache()
    52  	if err != nil {
    53  		return
    54  	}
    55  
    56  	return cache.Identity, nil
    57  }
    58  
    59  // StoreIdentity stores identity to cache
    60  func (ic *IdentityCache) StoreIdentity(identity Identity) error {
    61  	cache := cacheData{
    62  		Identity: identity,
    63  	}
    64  
    65  	return ic.writeCache(cache)
    66  }
    67  
    68  func (ic *IdentityCache) cacheExists() bool {
    69  	if _, err := os.Stat(ic.File); os.IsNotExist(err) {
    70  		return false
    71  	}
    72  
    73  	return true
    74  }
    75  
    76  func (ic *IdentityCache) readCache() (cache *cacheData, err error) {
    77  	data, err := os.ReadFile(ic.File)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	err = json.Unmarshal(data, &cache)
    83  	if err != nil {
    84  		return
    85  	}
    86  
    87  	return
    88  }
    89  
    90  func (ic *IdentityCache) writeCache(cache cacheData) (err error) {
    91  	cacheString, err := json.Marshal(cache)
    92  	if err != nil {
    93  		return
    94  	}
    95  
    96  	err = os.WriteFile(ic.File, cacheString, 0644)
    97  	return
    98  }