在我的部署过程中,我正在运行我的种子文件。我希望它是幂等的,这样我就可以多次运行它而不会出现任何问题。
目前,如果我多次运行它,我会收到 PG 主键错误。
我的种子模式如下所示:
user = User.create(.....)
user.save!
foo = Foo.create(....)
foo.save!
Run Code Online (Sandbox Code Playgroud)
我怎样才能使这个幂等?
这是最好的方法吗?
if( user.exists?(some_column: some_value) )
else
# do insert here
end
Run Code Online (Sandbox Code Playgroud) 是否有正式的区域名称列表用于以下内容:
zoneId = ZoneId.of("America/New_York")
Run Code Online (Sandbox Code Playgroud)
或者java.time.ZoneId是否可以生成我可以使用的列表本身?
我正在使用它将Instant转换为时间的字符串表示形式,如:
ZoneDateTime.ofInstant(instant, zoneId).format(DateTimeFormatter.ofPattern("..."))
Run Code Online (Sandbox Code Playgroud) 在我的AuthMw中间件中,我想进行数据库调用.
我的数据库变量在main中初始化,如何将其传递给我的中间件AuthMw?
func main() {
db, err := gorm.Open("postgres", ... )
r := mux.NewRouter()
r.Handle("/ws", serveWebsocket(hub))
r.Use(AuthMw)
//
//
...
} // main
func AuthMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
fmt.Printf("AuthMiddleware token is: %v\n", token)
ctx := ....
next.ServeHTTP(w, r.WithContext(ctx))
})
}
Run Code Online (Sandbox Code Playgroud) 我在我的 redux 应用程序中组合了多个减速器。
const reducers = combineReducers({
reducer1,
reducer2,
reducer3
});
Run Code Online (Sandbox Code Playgroud)
当用户执行注销时,如何删除该用户的整个 redux 状态?
当我在控制器的操作中进行以下调用时,我可以看到我第一次加载页面时在控制台输出中看到输出“posts hit the db”。
cache_key = "posts"
@posts = Rails.cache.fetch(cache_key, expires_in: 5.minutes) do
puts "posts hitting the db"
Post.include(:tags).where("post_status = 1").order("id desc")
end
Run Code Online (Sandbox Code Playgroud)
如果我重新加载页面,我不会看到“posts hit the db”消息,但我仍然可以看到如下查询:
由 PostsController#index 处理为 HTML 渲染 posts/index.html.erb 内布局/主帖子加载(0.5ms) SELECT "posts".* FROM "posts" WHERE (post_status = 1) ORDER BY id desc ?app/views/posts/index.html.erb:6 标签加载 (0.3ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 [["id", 7]] ? app/views/posts/index.html.erb:6 在布局/帖子中渲染的帖子/index.html.erb (31.4ms) 在 87ms 内完成 200 OK (Views: 62.6ms | ActiveRecord: 8.2ms)
这是因为它正在缓存 @posts 但由于 @posts 对象实际上没有被使用,它甚至没有进行 db 调用?那么当视图页面执行 …
我有一个 akka actor,它使用基于此 TCP 连接示例的经典风格:https://doc.akka.io/docs/akka/current/io-tcp.html
我正在尝试从 akka 类型化应用程序内部实例化这个 actor(因为我找不到使用类型化的完全相同的 TCP 连接的示例 - 不确定 API 是否可用?)
implicit val system = ActorSystem(SomeActor(), "typed-actorSystem") // import akka.actor.typed.ActorSystem
val remote = new InetSocketAddress(config.host, config.port)
val client =
system.classicSystem.actorOf(Client.props(remote), "clientActor")
Run Code Online (Sandbox Code Playgroud)
错误:
) java.lang.UnsupportedOperationException: 无法使用自定义用户监护人在 ActorSystem 上从外部创建顶级 actor [clientActor] [错误] java.lang.UnsupportedOperationException: 无法使用自定义用户在 ActorSystem 上从外部创建顶级 actor [clientActor]用户监护人
我怎样才能解决这个问题?我必须在其他地方实例化这个演员吗?
我目前正在尝试使用 notions API 下载网站/页面。我分享了该页面并尝试了以下操作:
(async () => {
const pageId = 'abcd-editorial-efe.notion.site/Sites-abc123';
const response = await notion.pages.retrieve({ page_id: pageId });
console.log(response);
})();
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
@notionhq/client warn: request fail {
code: 'object_not_found',
message: 'Could not find page with ID: abc123..... Make sure the relevant pages and databases are shared with your integration.'
}
Run Code Online (Sandbox Code Playgroud)
这是检索页面的正确 API 端点吗?
Notions docs pageId 是一个 GUID...但是当我共享我的页面时,我得到了一个完整的 URL:
(async () => {
const pageId = 'b55c9c91-384d-452b-81db-d1ef79372b75';
const response = await notion.pages.retrieve({ page_id: pageId });
console.log(response);
})();
Run Code Online (Sandbox Code Playgroud)
我在用 "@notionhq/client": …
我想学习如何用 Rust 编写这段 Go 代码,这里的 go 代码可供参考: https: //go.dev/play/p/j9osOG5xs1R
它基本上启动 100 个线程,每个线程循环 1000 次,每次迭代休眠 1 毫秒,并且还会增加一些共享状态。
由于它休眠 1 毫秒,因此应该在 1 秒内完成。
在我的 Go 版本中,我实际上创建了 100 个线程,并且按预期在大约 1 秒内完成。
我的 Rust 代码目前只有 10 个线程,需要 10 秒才能完成。
我一定做错了什么,有什么提高性能的建议吗?
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
use std::time::Instant;
let now = Instant::now();
for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = …Run Code Online (Sandbox Code Playgroud) 我想构建一个很好的API(C#),让人们更容易消费,我想我以前见过这个并想知道如何做到这一点:
MyNamespace.Cars car = null;
if(someTestCondition)
car = new Honda();
else
car = new Toyota();
car.Drive(40);
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,需要做什么?