今天大哥和我说公司的Java框架不能忍了,公交车轨迹回放及其的卡顿,数据量巨大,Java也不优化,给我一周时间让我调查.net core 可不可以做(你确定一周时间能从零调查?),闲来无事梭哈了一下,我想的是用MongoDB缓存历史数据,只保留七天,在时间字段上创建索引(TTL,设置7天过期,自动删除),前端轨迹回放时历史的从MongoDB查询,当天从oracle查询,oracle使用efcore交互,MongoDB使用MongoDB.Driver进行ODM映射并实现增删改查(实际业务只查询)。并使用log4net记录日志,集成swagger实现接口文档。下图是需要安装的依赖包
源码以及上传git(github搜索xdqt),这里只说明一点,如下
public class Book
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
public string BookName { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public string Author { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime InsertTime { get; set; }
}
复制
这里需要注意的是,InsertTime我们存的是本地时间,但保存到MongoDB后,会有8小时的时差,需要在这里声明反序列化时使用本地时间,所有的服务类均采用依赖注入的方式。
Oracle EFCore
在EF Core中,DbContext有一个名为onconfiguration的虚拟方法,它会被EF Core内部调用,它也会传递一个optionsBuilder实例,可以使用那个optionsBuilder来为DbContext配置选项。
optionsBuilder有UseOracle方法;参数为连接oracle数据库的字符串。
using HLZDVideoServer.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HLZDVideoServer.EntityOracleDBContext
{
public class EntityOracleDBContext : DbContext
{
public static readonly LoggerFactory MyLoggerFactory
= new LoggerFactory(new[] { new DebugLoggerProvider() });
public EntityOracleDBContext(DbContextOptions<EntityOracleDBContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<BB_FGSSC>();
//modelBuilder.Entity<BB_FGSSC>().ToTable("BB_FGSSC");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseLoggerFactory(MyLoggerFactory);
optionsBuilder.UseOracle(@"User Id=****;Password=****;Data Source=****:****/servicename");
}
public DbSet<BB_FGSSC> BB_FGSSCs { get; set; }
}
}
复制
entity
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace HLZDVideoServer.Entities
{
//指定表名
[Table("BB_FGSSC")]
public class BB_FGSSC
{
[Key]//表示这个字段是主键
public int FGSID { get; set; }
public string RQ { get; set; }
public string ZBZ { get; set; }
public string YYJL { get; set; }
public string WEATHER { get; set; }
}
}
复制
这仅仅是测试数据,正式会比这个复杂,但也会只是一个类而已,只不过会有几十个字段。
至于MongoDB和log4net就很简单了,这里只列举如何创建时间索引:
from pymongo import MongoClient
from datetime import datetime
conn=MongoClient('localhost',27017)
db=conn.BookstoreDb
#设置30秒后自动删除
db.Books.create_index("InsertTime",expireAfterSeconds=30)
复制
至此一个简单的API雏形框架就OK了,添加个定时任务,定时的把历史数据灌入MongoDB就可以了
文章转载自小兜全糖,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【纯干货】Oracle 19C RU 19.27 发布,如何快速升级和安装?
Lucifer三思而后行
770次阅读
2025-04-18 14:18:38
Oracle RAC 一键安装翻车?手把手教你如何排错!
Lucifer三思而后行
652次阅读
2025-04-15 17:24:06
Oracle数据库一键巡检并生成HTML结果,免费脚本速来下载!
陈举超
578次阅读
2025-04-20 10:07:02
【ORACLE】你以为的真的是你以为的么?--ORA-38104: Columns referenced in the ON Clause cannot be updated
DarkAthena
536次阅读
2025-04-22 00:13:51
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
524次阅读
2025-04-17 17:02:24
【ORACLE】记录一些ORACLE的merge into语句的BUG
DarkAthena
501次阅读
2025-04-22 00:20:37
一页概览:Oracle GoldenGate
甲骨文云技术
486次阅读
2025-04-30 12:17:56
火焰图--分析复杂SQL执行计划的利器
听见风的声音
456次阅读
2025-04-17 09:30:30
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
381次阅读
2025-04-15 14:48:05
OR+DBLINK的关联SQL优化思路
布衣
377次阅读
2025-05-05 19:28:36