123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using Cksoft.Unity;
- using DllEapCommon.Redis;
- using DllEapEntity;
- using LazyCache;
- using Microsoft.AspNetCore.Mvc;
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading.Tasks;
- namespace WebMainFrameForEap.Controllers
- {
- [ApiController]
- [Route("eap/api/redis/[action]")]
- public class RedisController : Controller
- {
- private readonly IDatabase _db;
- private readonly IAppCache _appCache;
- public RedisController(RedisHelper redisHelper, IAppCache appCache)
- {
- _db = redisHelper.GetDatabase();
- _appCache = appCache;
- }
- public async Task<string> Get()
- {
- var value = await _db.StringGetAsync(new RedisKey("name"));
- _appCache.Add<Machine>("testmac", new Machine { ID = 2, FCode = "AA00001" });
- var mac = await _appCache.GetAsync<Machine>("testmac");
- return mac.ToJson();
- }
- public async Task<string> Set(string value)
- {
- await _db.StringSetAsync(new RedisKey("name"), new RedisValue(value));
- return "ok";
- }
- public async Task<string> SetTableRows()
- {
- var dt = new DataTable();
- dt.Columns.Add("Id");
- dt.Columns.Add("FCode");
- dt.Columns.Add("FName");
- dt.Columns.Add("SupplierCode");
- for (int i = 0; i < 2; i++)
- {
- var row = dt.NewRow();
- row["Id"] = i + 1;
- row["FCode"] = "FCode" + i + 1;
- row["FName"] = "FName" + i + 1;
- row["SupplierCode"] = "SupplierCode" + i + 1;
- dt.Rows.Add(row);
- }
- foreach (DataRow row in dt.Rows)
- {
- var hashEntries = new List<HashEntry>();
- foreach (DataColumn col in dt.Columns)
- {
- var tmp = new HashEntry(col.ColumnName, row[col.ColumnName].ToString());
- hashEntries.Add(tmp);
- }
- await _db.HashSetAsync("MacModel" + row["Id"].ToString(), hashEntries.ToArray());
- }
- return "ok";
- }
- public async Task<string> GetHashRow()
- {
- var vals = await _db.HashGetAllAsync("MacModel1");
- var str = string.Empty;
- foreach (var item in vals)
- {
- str += item.Name + "=" + item.Value + ",";
- }
- return str;
- }
- }
- }
|