github.com/Ptt-official-app/go-bbs@v0.12.0/cache/cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  type Cache interface {
    10  	Bytes() []byte
    11  	Close() error
    12  }
    13  
    14  // NewCache returns Cache (SHM) by connectionString, connectionString indicate the shm location
    15  // with uri format  eg. shmkey:1228 or file:/tmp/ramdisk/bbs.shm
    16  func NewCache(connectionString string) (Cache, error) {
    17  	s := strings.Split(connectionString, ":")
    18  	if len(s) == 1 {
    19  		// default is mmap
    20  		return OpenMmap(s[0])
    21  	}
    22  	scheme := s[0]
    23  	switch scheme {
    24  	case "shmkey":
    25  		key, err := strconv.Atoi(s[1])
    26  		if err != nil {
    27  			return nil, fmt.Errorf("atoi error: %w", err)
    28  		}
    29  		return OpenKey(key)
    30  	case "file":
    31  		return OpenMmap(s[1])
    32  	default:
    33  		return nil, fmt.Errorf("unsupport scheme: %v", scheme)
    34  	}
    35  
    36  }