Beispiel #1
0
func GetHandler() VisitsHandler {
	db, err := kc.Open("/tmp/visits.kch", kc.WRITE)
	if err != nil {
		log.Fatal(err)
	}
	return VisitsHandler{db: db}
}
Beispiel #2
0
func NewShortener(g Generator, urlsDbName, visitsDbName string) (*Shortener, error) {
	urlsDb, err := kc.Open(urlsDbName, kc.WRITE)
	if err != nil {
		return nil, err
	}

	visitsDb, err := kc.Open(visitsDbName, kc.WRITE)
	if err != nil {
		return nil, err
	}

	s := &Shortener{
		g:        g,
		urlsDb:   urlsDb,
		visitsDb: visitsDb,
	}

	return s, nil
}
Beispiel #3
0
func main() {
	db, err := kc.Open("/tmp/cache.kch", kc.WRITE)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	db.Set("names", "Maria|João|José")
	db.SetInt("hits", 500)
	for i := 0; i < 100; i++ {
		db.Increment("hits", 1)
	}
}
Beispiel #4
0
func TestShortenerShouldStoreInternalInstanceOfTheVisitsDatabaseInWriteMode(t *testing.T) {
	tmpDb, _ := kc.Open("/tmp/removeme.kch", kc.WRITE)
	defer tmpDb.Close()

	g := GeneratorMock("random")
	shortener, _ := NewShortener(&g, "/tmp/urls.kch", "/tmp/visits.kch")
	defer shortener.CloseDbs()

	typeVisitsDb := reflect.TypeOf(shortener.visitsDb)
	if typeVisitsDb != reflect.TypeOf(tmpDb) || shortener.visitsDb == nil {
		t.Errorf("Should store the visits database instance with the given name")
	}
}