DataService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using IMCS.CCS.Common;
  2. using IMCS.CCS.Common.Redis;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. namespace IMCS.CCS.Service.Impl
  10. {
  11. /// <summary>
  12. /// 数据服务
  13. /// </summary>
  14. public class DataService : IDataService
  15. {
  16. private readonly string _webRootPath;
  17. private string PathOrKey { get; set; }
  18. private readonly IDataStorageConfigurationService _dataStorageConfigurationService;
  19. private readonly IRedisService _redisService;
  20. public DataService(IWebHostEnvironment webHostEnvironment, IDataStorageConfigurationService dataStorageConfigurationService, IRedisService redisService)
  21. {
  22. _webRootPath = webHostEnvironment.WebRootPath;
  23. _dataStorageConfigurationService = dataStorageConfigurationService;
  24. _redisService = redisService;
  25. }
  26. public bool Init(string pathOrKey)
  27. {
  28. PathOrKey = pathOrKey;
  29. return true;
  30. }
  31. /// <summary>
  32. /// 读取数据
  33. /// </summary>
  34. /// <typeparam name="T"></typeparam>
  35. /// <returns></returns>
  36. public async Task<IEnumerable<T>> ReadDataAsync<T>()
  37. {
  38. var data = string.Empty;
  39. if (_dataStorageConfigurationService.IsUseRedis())
  40. {
  41. data = await this._redisService.Database.StringGetAsync(PathOrKey);
  42. }
  43. else
  44. {
  45. try
  46. {
  47. var path = this.GetPath();
  48. //using var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  49. //var buffer = new byte[fs.Length];
  50. //fs.Position = 0;
  51. //await fs.ReadAsync(buffer, 0, buffer.Length);
  52. //var data = Encoding.UTF8.GetString(buffer);
  53. //if (string.IsNullOrWhiteSpace(data)) return default;
  54. data = await File.ReadAllTextAsync(path);
  55. }
  56. catch (Exception ex)
  57. {
  58. Console.WriteLine("读取文件失败====》" + ex.Message);
  59. //return await ReadDataAsync<T>();
  60. }
  61. }
  62. return string.IsNullOrWhiteSpace(data) ? default : JsonConvert.DeserializeObject<IEnumerable<T>>(data);
  63. }
  64. /// <summary>
  65. /// 写入数据
  66. /// </summary>
  67. /// <returns></returns>
  68. public async Task<bool> WriteDataAsync<T>(T contents)
  69. {
  70. var json = JsonConvert.SerializeObject(contents);
  71. if (_dataStorageConfigurationService.IsUseRedis())
  72. {
  73. await this._redisService.Database.StringSetAsync(PathOrKey, json);
  74. }
  75. else
  76. {
  77. try
  78. {
  79. var path = this.GetPath();
  80. //using var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  81. //fs.Seek(0, SeekOrigin.Begin);
  82. //fs.SetLength(0);
  83. //var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(contents));
  84. //fs.Position = 0;
  85. //await fs.WriteAsync(data, 0, data.Length);
  86. await File.WriteAllTextAsync(path, json);
  87. }
  88. catch (Exception ex)
  89. {
  90. Console.WriteLine("更新JSON文件失败==>"+ex.Message);
  91. //return await WriteDataAsync(contents);
  92. }
  93. }
  94. return true;
  95. }
  96. #region 私有
  97. private string GetPath()
  98. {
  99. if (string.IsNullOrWhiteSpace(PathOrKey)) throw new MessageBox("属性 FilePath 空对象,请使用前调用 Init 函数!");
  100. var path = _webRootPath + PathOrKey;
  101. if (!File.Exists(path))
  102. {
  103. File.Create(path);
  104. }
  105. return path;
  106. }
  107. #endregion
  108. }
  109. }