Beispiel #1
0
// Query implements DB.QueryNoCache. It *always* hits the database and updates the cache.
func (c *Cache) QueryNoCache(query string) (*data.Frame, error) {
	name := tableName(query)
	c.logf("caching [%s] %.200s", name, query)
	if err := c.saveQuery(query); err != nil {
		c.purgeQuery(name)
		return nil, err
	}
	return data.ReadFromDB(c.cache, "SELECT * FROM "+name)
}
Beispiel #2
0
// staleQueries returns all queries that haven't being accessed in the lastAccessed seconds.
func staleQueries(db *sql.DB, lastAccessed int) (*data.Frame, error) {
	const stale = `
	SELECT DISTINCT
		table_name
	FROM
		(SELECT table_name, MAX(time) as last_access FROM log GROUP BY table_name) a
	WHERE
		last_access <= datetime('now', '-%d seconds')`
	return data.ReadFromDB(db, fmt.Sprintf(stale, lastAccessed))
}
Beispiel #3
0
// Query runs a query and returns a DataFrame
func (s *SQL) Query(query string) (*data.Frame, error) {
	df, err := data.ReadFromDB(s.db, query)
	if err != nil {
		return nil, err
	}
	if s.driver == "mysql" {
		df.ConvertTime()
		df.ConvertNumeric()
	}
	return df, nil
}
Beispiel #4
0
// outdatedQueries returns all queries that were not updated in the lastUpdated seconds.
func outdatedQueries(db *sql.DB, lastUpdated int) (*data.Frame, error) {
	const outdated = `
	SELECT 
		q.table_name, q.sql
	FROM 
		queries q
			LEFT JOIN
		(SELECT table_name, MAX(time) AS last_updated FROM log WHERE duration != -1 GROUP BY table_name) u
			ON q.table_name = u.table_name
	WHERE
		last_updated IS NULL OR last_updated <= datetime('now', '-%d seconds')`
	return data.ReadFromDB(db, fmt.Sprintf(outdated, lastUpdated))
}
Beispiel #5
0
// Query implements DB.Query. If query was cached before, this result is returned; otherwise
// underlying DB is queried and the result is stored in the cache first.
func (c *Cache) Query(query string) (*data.Frame, error) {
	name := tableName(query)
	if cacheExists(c.cache, name) {
		c.logf("cache hit for [%s] %.200s", name, query)
		// Cache access that doesn't hit the underlying DB is logged with negative duration.
		c.lock.Lock()
		defer c.lock.Unlock()
		if err := logCacheAccess(c.cache, name, time.Now(), time.Duration(-1)); err != nil {
			return nil, err
		}
		return data.ReadFromDB(c.cache, "SELECT * FROM "+name)
	}
	return c.QueryNoCache(query)
}