This commit is contained in:
2026-02-11 17:46:22 +03:00
commit eacfacb13b
266 changed files with 51337 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
package authz
import (
"database/sql"
"fmt"
"sync"
)
// =====================================================
// 🧠 PIYASA CACHE (USER → CODES)
// =====================================================
var (
piyasaCache = make(map[int][]string)
piyasaMu sync.RWMutex
)
// =====================================================
// 📌 GET USER PIYASA CODES (CACHED)
// =====================================================
func GetUserPiyasaCodes(pg *sql.DB, userID int) ([]string, error) {
// -----------------------------
// CACHE READ
// -----------------------------
piyasaMu.RLock()
if it, ok := piyasaCache[userID]; ok {
piyasaMu.RUnlock()
return it, nil
}
piyasaMu.RUnlock()
// -----------------------------
// DB QUERY
// -----------------------------
rows, err := pg.Query(`
SELECT piyasa_code
FROM dfusr_piyasa
WHERE dfusr_id = $1
AND is_allowed = true
`, userID)
if err != nil {
return nil, fmt.Errorf("pg piyasa query error: %w", err)
}
defer rows.Close()
var out []string
for rows.Next() {
var code string
if err := rows.Scan(&code); err == nil {
out = append(out, code)
}
}
// -----------------------------
// CACHE WRITE
// -----------------------------
piyasaMu.Lock()
piyasaCache[userID] = out
piyasaMu.Unlock()
return out, nil
}
// =====================================================
// 🧹 CLEAR USER PIYASA CACHE
// =====================================================
func ClearPiyasaCache(userID int) {
piyasaMu.Lock()
defer piyasaMu.Unlock()
delete(piyasaCache, userID)
}