Git Product home page Git Product logo

safsm's Introduction

safsm - Simple As F⭐️ck Session Manager

Installation

go get -u github.com/nyan2d/safsm

Session storage

We have several session storage types: MemoryStorage, FileStorage, SQLiteStorage.

But you can implement your own type using the interface below:

type Storage interface {
    Close() error
    InsertSession(session *Session) error
    FindSession(token string) (*Session, error)
    RemoveSession(token string) error
    Each(f func(session *Session))
}

Cooking

Create a session manager

// open sqlite database
db, err := sql.Open("sqlite", "sessions.db")
if err != nil {
    ...
}

// create storage
storage, err := safsm.NewSQLiteStorage(db)
if err != nil {
    ...
}

// create session manager
sm := safsm.New(storage)

Create a session

ttl := time.Minute * 60
session := sm.CreateSession(userID, ttl)

Playing with cookies

Write a session to cookies

func demoHandler (w http.ResponseWriter, r *http.Request) {
    session.SetCookie(w)
}

Read session from cookies

func demoHandler (w http.ResponseWriter, r *http.Request) {
    session, err := safsm.ReadSession(r)
    if err == safsm.ErrNoSession {
        // there is no session
    }
}

Caching

Caching is implemented by a storage-wrapper with the type CachedStorage

cacheSize := 1024
cachedStorage := safsm.NewCachedStorage(originalStorage, cacheSize)

Warning! Cache invalidation works very simply: if the number of sessions hit by the cache exceeds the cache size, the entire cache is just invalidated.

This behavior can be reworked in the future.

Assigning session to session manager

If you want to save a session from a storage you work with, you need to assign the session to some session manager

session.AssignTo(sm)
session.Save()

Session functions chaining

Some of session funcs can be chained. For example:

session.AssignTo(sm).
    Update().
    SetTTL(time.Minute * 60).
    Save()

Session lifetime

A session has the following time parameters:

  • CreatedAt
  • UpdatedAt
  • TTL

If the current time exceeds the session update time + TTL, the session is considered invalid. If you need to update the session, you can use the following approach:

if session.TimeToLive().Minutes() < 60 {
    session.Update().AssignTo(sm).Save()
}

License

The code is distributed under the MIT license.

safsm's People

Contributors

nyan2d avatar

Watchers

Lucian avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.