<commit_before>// Copyright 2015-2016 <NAME>. All rights reserved.
// Use of this source code is governed by a AGPL
// license that can be found in the LICENSE file.

// cache.go handles the global variables for caching and the clearing.

package main

import (
	"strings"
	"sync"
	"time"
)

var psCache = struct {
	sync.RWMutex
	m map[string]FullParameters
}{m: make(map[string]FullParameters)}

var usersCache = struct {
	sync.RWMutex
	m map[string][]string
}{m: make(map[string][]string)}

var userPositionCache = struct {
	sync.RWMutex
	m map[string]UserPositionJSON
}{m: make(map[string]UserPositionJSON)}

var isLearning = struct {
	sync.RWMutex
	m map[string]bool
}{m: make(map[string]bool)}

func init() {
	go clearCache()
}

func clearCache() {
	for {
		Debug.Println("Resetting cache")
		go resetCache("isLearning")
		go resetCache("psCache")
		go resetCache("userCache")
		go resetCache("userPositionCache")
		time.Sleep(time.Second * 10)
	}
}

func resetCache(cache string) {
	if cache == "userCache" {
		usersCache.Lock()
		usersCache.m = make(map[string][]string)
		usersCache.Unlock()
	} else if cache == "userPositionCache" {
		userPositionCache.Lock()
		userPositionCache.m = make(map[string]UserPositionJSON)
		userPositionCache.Unlock()
	} else if cache == "psCache" {
		psCache.Lock()
		psCache.m = make(map[string]FullParameters)
		psCache.Unlock()
	} else if cache == "isLearning" {
		isLearning.Lock()
		isLearning.m = make(map[string]bool)
		isLearning.Unlock()
	}
}

func getLearningCache(group string) (bool, bool) {
	Debug.Println("getLearningCache")
	isLearning.RLock()
	cached, ok := isLearning.m[group]
	isLearning.RUnlock()
	return cached, ok
}

func setLearningCache(group string, val bool) {
	isLearning.Lock()
	isLearning.m[group] = val
	isLearning.Unlock()
}

func getUserCache(group string) ([]string, bool) {
	Debug.Println("Getting userCache")
	usersCache.RLock()
	cached, ok := usersCache.m[group]
	usersCache.RUnlock()
	return cached, ok
}

func setUserCache(group string, users []string) {
	usersCache.Lock()
	usersCache.m[group] = users
	usersCache.Unlock()
}

func appendUserCache(group string, user string) {
	usersCache.Lock()
	if _, ok := usersCache.m[group]; ok {
		if len(usersCache.m[group]) == 0 {
			usersCache.m[group] = append([]string{}, strings.ToLower(user))
		}
	}
	usersCache.Unlock()
}

func getPsCache(group string) (FullParameters, bool) {
	Debug.Println("Getting pscache")
	psCache.RLock()
	psCached, ok := psCache.m[group]
	psCache.RUnlock()
	return psCached, ok
}

func setPsCache(group string, ps FullParameters) {
	Debug.Println("Setting pscache")
	psCache.Lock()
	psCache.m[group] = ps
	psCache.Unlock()
	return
}

func getUserPositionCache(group string) (UserPositionJSON, bool) {
	Debug.Println("getUserPositionCache")
	userPositionCache.RLock()
	cached, ok := userPositionCache.m[group]
	userPositionCache.RUnlock()
	return cached, ok
}

func setUserPositionCache(group string, p UserPositionJSON) {
	Debug.Println("setUserPositionCache")
	userPositionCache.Lock()
	userPositionCache.m[group] = p
	userPositionCache.Unlock()
	return
}
<commit_msg>Cache mapping for everything to Fix #105<commit_after>// Copyright 2015-2016 <NAME>. All rights reserved.
// Use of this source code is governed by a AGPL
// license that can be found in the LICENSE file.

// cache.go handles the global variables for caching and the clearing.

package main

import (
	"strings"
	"sync"
	"time"
)

var psCache = struct {
	sync.RWMutex
	m map[string]FullParameters
}{m: make(map[string]FullParameters)}

var usersCache = struct {
	sync.RWMutex
	m map[string][]string
}{m: make(map[string][]string)}

var userPositionCache = struct {
	sync.RWMutex
	m map[string]UserPositionJSON
}{m: make(map[string]UserPositionJSON)}

var isLearning = struct {
	sync.RWMutex
	m map[string]bool
}{m: make(map[string]bool)}

func init() {
	go clearCache()
}

func clearCache() {
	for {
		//Debug.Println("Resetting cache")
		go resetCache("isLearning")
		go resetCache("psCache")
		go resetCache("userCache")
		go resetCache("userPositionCache")
		time.Sleep(time.Second * 10)
	}
}

func resetCache(cache string) {
	if cache == "userCache" {
		usersCache.Lock()
		usersCache.m = make(map[string][]string)
		usersCache.Unlock()
	} else if cache == "userPositionCache" {
		userPositionCache.Lock()
		userPositionCache.m = make(map[string]UserPositionJSON)
		userPositionCache.Unlock()
	} else if cache == "psCache" {
		psCache.Lock()
		psCache.m = make(map[string]FullParameters)
		psCache.Unlock()
	} else if cache == "isLearning" {
		isLearning.Lock()
		isLearning.m = make(map[string]bool)
		isLearning.Unlock()
	}
}

func getLearningCache(group string) (bool, bool) {
	//Debug.Println("getLearningCache")
	isLearning.RLock()
	cached, ok := isLearning.m[group]
	isLearning.RUnlock()
	return cached, ok
}

func setLearningCache(group string, val bool) {
	isLearning.Lock()
	isLearning.m[group] = val
	isLearning.Unlock()
}

func getUserCache(group string) ([]string, bool) {
	//Debug.Println("Getting userCache")
	usersCache.RLock()
	cached, ok := usersCache.m[group]
	usersCache.RUnlock()
	return cached, ok
}

func setUserCache(group string, users []string) {
	usersCache.Lock()
	usersCache.m[group] = users
	usersCache.Unlock()
}

func appendUserCache(group string, user string) {
	usersCache.Lock()
	if _, ok := usersCache.m[group]; ok {
		if len(usersCache.m[group]) == 0 {
			usersCache.m[group] = append([]string{}, strings.ToLower(user))
		}
	}
	usersCache.Unlock()
}

func getPsCache(group string) (FullParameters, bool) {
	//Debug.Println("Getting pscache")
	psCache.RLock()
	psCached, ok := psCache.m[group]
	psCache.RUnlock()
	return psCached, ok
}

func setPsCache(group string, ps FullParameters) {
	//Debug.Println("Setting pscache")
	psCache.Lock()
	psCache.m[group] = ps
	psCache.Unlock()
	return
}

func getUserPositionCache(group string) (UserPositionJSON, bool) {
	//Debug.Println("getUserPositionCache")
	userPositionCache.RLock()
	cached, ok := userPositionCache.m[group]
	userPositionCache.RUnlock()
	return cached, ok
}

func setUserPositionCache(group string, p UserPositionJSON) {
	//Debug.Println("setUserPositionCache")
	userPositionCache.Lock()
	userPositionCache.m[group] = p
	userPositionCache.Unlock()
	return
}

{'fraction_non_alphanumeric': 0.1349800796812749, 'fraction_numerical': 0.00398406374501992, 'mean_word_length': 5.08729388942774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5742679', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2162, 'n_words': 492}