github.com/astaxie/beego@v1.12.3/session/couchbase/sess_couchbase.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package couchbase for session provider
    16  //
    17  // depend on github.com/couchbaselabs/go-couchbasee
    18  //
    19  // go install github.com/couchbaselabs/go-couchbase
    20  //
    21  // Usage:
    22  // import(
    23  //   _ "github.com/astaxie/beego/session/couchbase"
    24  //   "github.com/astaxie/beego/session"
    25  // )
    26  //
    27  //	func init() {
    28  //		globalSessions, _ = session.NewManager("couchbase", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"http://host:port/, Pool, Bucket"}``)
    29  //		go globalSessions.GC()
    30  //	}
    31  //
    32  // more docs: http://beego.me/docs/module/session.md
    33  package couchbase
    34  
    35  import (
    36  	"net/http"
    37  	"strings"
    38  	"sync"
    39  
    40  	couchbase "github.com/couchbase/go-couchbase"
    41  
    42  	"github.com/astaxie/beego/session"
    43  )
    44  
    45  var couchbpder = &Provider{}
    46  
    47  // SessionStore store each session
    48  type SessionStore struct {
    49  	b           *couchbase.Bucket
    50  	sid         string
    51  	lock        sync.RWMutex
    52  	values      map[interface{}]interface{}
    53  	maxlifetime int64
    54  }
    55  
    56  // Provider couchabse provided
    57  type Provider struct {
    58  	maxlifetime int64
    59  	savePath    string
    60  	pool        string
    61  	bucket      string
    62  	b           *couchbase.Bucket
    63  }
    64  
    65  // Set value to couchabse session
    66  func (cs *SessionStore) Set(key, value interface{}) error {
    67  	cs.lock.Lock()
    68  	defer cs.lock.Unlock()
    69  	cs.values[key] = value
    70  	return nil
    71  }
    72  
    73  // Get value from couchabse session
    74  func (cs *SessionStore) Get(key interface{}) interface{} {
    75  	cs.lock.RLock()
    76  	defer cs.lock.RUnlock()
    77  	if v, ok := cs.values[key]; ok {
    78  		return v
    79  	}
    80  	return nil
    81  }
    82  
    83  // Delete value in couchbase session by given key
    84  func (cs *SessionStore) Delete(key interface{}) error {
    85  	cs.lock.Lock()
    86  	defer cs.lock.Unlock()
    87  	delete(cs.values, key)
    88  	return nil
    89  }
    90  
    91  // Flush Clean all values in couchbase session
    92  func (cs *SessionStore) Flush() error {
    93  	cs.lock.Lock()
    94  	defer cs.lock.Unlock()
    95  	cs.values = make(map[interface{}]interface{})
    96  	return nil
    97  }
    98  
    99  // SessionID Get couchbase session store id
   100  func (cs *SessionStore) SessionID() string {
   101  	return cs.sid
   102  }
   103  
   104  // SessionRelease Write couchbase session with Gob string
   105  func (cs *SessionStore) SessionRelease(w http.ResponseWriter) {
   106  	defer cs.b.Close()
   107  
   108  	bo, err := session.EncodeGob(cs.values)
   109  	if err != nil {
   110  		return
   111  	}
   112  
   113  	cs.b.Set(cs.sid, int(cs.maxlifetime), bo)
   114  }
   115  
   116  func (cp *Provider) getBucket() *couchbase.Bucket {
   117  	c, err := couchbase.Connect(cp.savePath)
   118  	if err != nil {
   119  		return nil
   120  	}
   121  
   122  	pool, err := c.GetPool(cp.pool)
   123  	if err != nil {
   124  		return nil
   125  	}
   126  
   127  	bucket, err := pool.GetBucket(cp.bucket)
   128  	if err != nil {
   129  		return nil
   130  	}
   131  
   132  	return bucket
   133  }
   134  
   135  // SessionInit init couchbase session
   136  // savepath like couchbase server REST/JSON URL
   137  // e.g. http://host:port/, Pool, Bucket
   138  func (cp *Provider) SessionInit(maxlifetime int64, savePath string) error {
   139  	cp.maxlifetime = maxlifetime
   140  	configs := strings.Split(savePath, ",")
   141  	if len(configs) > 0 {
   142  		cp.savePath = configs[0]
   143  	}
   144  	if len(configs) > 1 {
   145  		cp.pool = configs[1]
   146  	}
   147  	if len(configs) > 2 {
   148  		cp.bucket = configs[2]
   149  	}
   150  
   151  	return nil
   152  }
   153  
   154  // SessionRead read couchbase session by sid
   155  func (cp *Provider) SessionRead(sid string) (session.Store, error) {
   156  	cp.b = cp.getBucket()
   157  
   158  	var (
   159  		kv  map[interface{}]interface{}
   160  		err error
   161  		doc []byte
   162  	)
   163  
   164  	err = cp.b.Get(sid, &doc)
   165  	if err != nil {
   166  		return nil, err
   167  	} else if doc == nil {
   168  		kv = make(map[interface{}]interface{})
   169  	} else {
   170  		kv, err = session.DecodeGob(doc)
   171  		if err != nil {
   172  			return nil, err
   173  		}
   174  	}
   175  
   176  	cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
   177  	return cs, nil
   178  }
   179  
   180  // SessionExist Check couchbase session exist.
   181  // it checkes sid exist or not.
   182  func (cp *Provider) SessionExist(sid string) bool {
   183  	cp.b = cp.getBucket()
   184  	defer cp.b.Close()
   185  
   186  	var doc []byte
   187  
   188  	if err := cp.b.Get(sid, &doc); err != nil || doc == nil {
   189  		return false
   190  	}
   191  	return true
   192  }
   193  
   194  // SessionRegenerate remove oldsid and use sid to generate new session
   195  func (cp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
   196  	cp.b = cp.getBucket()
   197  
   198  	var doc []byte
   199  	if err := cp.b.Get(oldsid, &doc); err != nil || doc == nil {
   200  		cp.b.Set(sid, int(cp.maxlifetime), "")
   201  	} else {
   202  		err := cp.b.Delete(oldsid)
   203  		if err != nil {
   204  			return nil, err
   205  		}
   206  		_, _ = cp.b.Add(sid, int(cp.maxlifetime), doc)
   207  	}
   208  
   209  	err := cp.b.Get(sid, &doc)
   210  	if err != nil {
   211  		return nil, err
   212  	}
   213  	var kv map[interface{}]interface{}
   214  	if doc == nil {
   215  		kv = make(map[interface{}]interface{})
   216  	} else {
   217  		kv, err = session.DecodeGob(doc)
   218  		if err != nil {
   219  			return nil, err
   220  		}
   221  	}
   222  
   223  	cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
   224  	return cs, nil
   225  }
   226  
   227  // SessionDestroy Remove bucket in this couchbase
   228  func (cp *Provider) SessionDestroy(sid string) error {
   229  	cp.b = cp.getBucket()
   230  	defer cp.b.Close()
   231  
   232  	cp.b.Delete(sid)
   233  	return nil
   234  }
   235  
   236  // SessionGC Recycle
   237  func (cp *Provider) SessionGC() {
   238  }
   239  
   240  // SessionAll return all active session
   241  func (cp *Provider) SessionAll() int {
   242  	return 0
   243  }
   244  
   245  func init() {
   246  	session.Register("couchbase", couchbpder)
   247  }