feat: 新增 Redis 缓存同步相关模块与控制器

This commit is contained in:
陈赓
2025-07-15 11:29:18 +08:00
parent cdbc277a94
commit 8e8f5eb8be
3 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
# study/jikimo_sf/sf_mrs_connect/controllers/sync_controller.py
from odoo import http
from odoo.http import request
class FixtureSyncController(http.Controller):
@http.route('/api/fixture_model/sync_from_mrs', type='json', auth='none', csrf=False)
def sync_model(self, **kw):
code = kw.get('code')
if not code:
return {'status':'fail','msg':'code missing'}
request.env['sf.fixture.model'].sudo().sync_from_mrs(code)
return {'status':'success'}
@http.route('/api/fixture_param/sync_from_mrs', type='json', auth='none', csrf=False)
def sync_param(self, **kw):
code = kw.get('code')
if not code:
return {'status':'fail','msg':'code missing'}
request.env['sf.fixture.materials.basic.parameters']\
.sudo().sync_from_mrs(code)
return {'status':'success'}

View File

@@ -0,0 +1,14 @@
# study/jikimo_sf/sf_mrs_connect/models/common.py
import time, hashlib
class Common:
@staticmethod
def get_headers(token, secret_key):
ts = str(int(time.time()))
sign = hashlib.sha256(f"{token}{secret_key}{ts}".encode()).hexdigest()
return {
"token": token,
"sign": sign,
"timestamp": ts,
"Content-Type": "application/json",
}

View File

@@ -0,0 +1,30 @@
# study/jikimo_sf/sf_mrs_connect/models/redis_utils.py
import redis, json, logging
_logger = logging.getLogger(__name__)
class RedisClient:
def __init__(self, host='localhost', port=6379, db=0):
try:
self.client = redis.Redis(host=host, port=port, db=db, decode_responses=True)
except Exception as e:
_logger.error(f"Redis init error: {e}")
self.client = None
def get_json(self, key):
if not self.client:
return None
try:
data = self.client.get(key)
return json.loads(data) if data else None
except Exception as e:
_logger.error(f"Redis GET error [{key}]: {e}")
return None
def set_json(self, key, value, ex=3600):
if not self.client:
return
try:
self.client.set(key, json.dumps(value, ensure_ascii=False), ex=ex)
except Exception as e:
_logger.error(f"Redis SET error [{key}]: {e}")