Exemplo n.º 1
0
func (b *CassandraBackend) UpdateCount(key string, period time.Duration, increment int64) error {
	activeTable, _ := b.tableNames()
	hitKey := timeutils.GetHit(b.UtcNow(), key, period)
	query := b.session.Query(
		fmt.Sprintf(
			"UPDATE %s SET value = value + ? WHERE hit = ?",
			activeTable),
		increment,
		hitKey)

	if err := query.Exec(); err != nil {
		glog.Errorf("Error when executing update query for %s, err: %s", hitKey, err)
		return err
	}
	return nil
}
Exemplo n.º 2
0
func (b *CassandraBackend) GetCount(key string, period time.Duration) (int64, error) {
	var counter int64
	hitKey := timeutils.GetHit(b.UtcNow(), key, period)
	activeTable, _ := b.tableNames()
	query := b.session.Query(
		fmt.Sprintf(
			"SELECT value from %s WHERE hit = ? LIMIT 1",
			activeTable),
		hitKey)

	if err := query.Scan(&counter); err != nil {
		if err == gocql.ErrNotFound {
			glog.Infof("Entry %s for %s not found, it's ok", key, hitKey)
			return 0, nil
		}
		glog.Errorf("Error when executing query, err: %s", err)
		return -1, err
	}
	glog.Infof("Hitkey: %s counter: %d", hitKey, counter)
	return counter, nil
}
Exemplo n.º 3
0
func (b *MemoryBackend) UpdateCount(key string, period time.Duration, increment int64) error {
	b.Hits[timeutils.GetHit(b.UtcNow(), key, period)] += increment
	return nil
}
Exemplo n.º 4
0
func (b *MemoryBackend) GetCount(key string, period time.Duration) (int64, error) {
	return b.Hits[timeutils.GetHit(b.UtcNow(), key, period)], nil
}