我有一个表作为 ad_banner_queue,我用它来生成基于广告权重的队列。广告被插入到广告表中。如果队列中的所有现有广告都已转换给用户,则会生成队列。
现在的问题是,如果请求同时出现并且 Rand() 返回了相同的记录,我应该如何防止发送重复的广告?
代码如下:
<?php
/* To Get the random Ad */
public function getBanner($params) {
/* Fetch the Random from table */
$ads_queue = (new \yii\db\Query())
->select('ad_quque_id, banner_image, unique_code')
->from('ad_banner_queue')
->join('inner join', 'advertisement', 'ad_banner_queue.ad_id = advertisement.ad_id')
->where('is_sent=0')
->orderBy('RAND()')
->one();
/* In case of queue is not there generate the new queue */
if ($ads_queue === false) {
$output = $this->generateAdQueue();
//In case of something went wrong while generating the queue
if ($output == false) {
return array();
}
//Now fetch the record again
$ads_queue = (new \yii\db\Query())
->select('ad_quque_id, banner_image, unique_code')
->from('ad_banner_queue')
->join('inner join', 'advertisement', 'ad_banner_queue.ad_id = advertisement.ad_id')
->where('is_sent=0')
->orderBy('RAND()')
->one();
}
/* Now, marked that one as is_sent */
Yii::$app->db->createCommand()->update('ad_banner_queue', ['is_sent' => 1], 'ad_quque_id =:ad_quque_id', array(':ad_quque_id' => $ads_queue['ad_quque_id']))->execute();
return $ads_queue;
}
/**
* Below will Generate the Queue if not exist
*/
public function generateAdQueue() {
/* First check thatt there is existing queue, if so don't generate it */
$data_exist = (new \yii\db\Query())
->select('ad_quque_id')
->from('ad_banner_queue')
->where('is_sent=0')
->scalar();
if ($data_exist === false) {
/* Delete all other entries */
(new \yii\db\Query())
->createCommand()
->delete('ad_banner_queue')
->execute();
/* Fetch all banner */
$ads = (new \yii\db\Query())
->select('ad_id, unique_code, ad_name, banner_image,ad_delivery_weightage')
->from('advertisement')
->where('status_id in (8)') //Means only fetch Approved ads
->all();
if (!empty($ads)) {
foreach ($ads as $ad) {
/* Make entry as per that weightage, example, if weightage is 10 then make entry 10 times */
$ins_fields = array();
for ($i = 1; $i <= $ad['ad_delivery_weightage']; $i++) {
$ins_fields[] = array($ad['ad_id']);
}
Yii::$app->db->createCommand()->batchInsert('ad_banner_queue', ['ad_id'], $ins_fields)->execute();
}
return true;
} else {
return false;
}
} else {
return false;
}
}
最佳答案
我的意思是您的意思是执行同时请求的不同“人”不应该得到相同的随机行?为了避免在两个正在运行的请求中两次选择同一条记录的微小机会,最可靠的方法可能是锁定表并在事务中执行读取和更新,而无需对其进行测试。您必须使用支持此功能的存储引擎,例如 InnoDB。
实现方式LOCK TABLES和 UNLOCK TABLES对于事务表,例如 InnoDB 表,是用 SET autocommit = 0 开始事务, 不是 START TRANSACTION , 其次是 LOCK TABLES .那你不应该调用UNLOCK TABLES直到您明确提交交易。
例如,如果您需要一次读取和写入您的表,您可以这样做:
SET autocommit = 0;
LOCK TABLES ad_banner_queue AS ad_banner_queue_w WRITE, ad_banner_queue AS ad_banner_queue_r READ;
... perform your select query on ad_banner_queue_r, then update that row in ad_banner_queue_w with is_sent = 1...
COMMIT;
UNLOCK TABLES;
我们使用别名锁定的原因是您 cannot refer to a locked table multiple times in a single query using the same name .所以我们改用别名,并为表和每个别名获得一个单独的锁。
关于php - 防止从 MySQL 和 PHP 发送重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52426582/
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).
s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成
我目前正在用Ruby编写一个项目,它使用ActiveRecordgem进行数据库交互,我正在尝试使用ActiveRecord::Base.logger记录所有数据库事件具有以下代码的属性ActiveRecord::Base.logger=Logger.new(File.open('logs/database.log','a'))这适用于迁移等(出于某种原因似乎需要启用日志记录,因为它在禁用时会出现NilClass错误)但是当我尝试运行包含调用ActiveRecord对象的线程守护程序的项目时脚本失败并出现以下错误/System/Library/Frameworks/Ruby.frame
我有一个应用需要发送用户事件邀请。当用户邀请friend(用户)参加事件时,如果尚不存在将用户连接到该事件的新记录,则会创建该记录。我的模型由用户、事件和events_user组成。classEventdefinvite(user_id,*args)user_id.eachdo|u|e=EventsUser.find_or_create_by_event_id_and_user_id(self.id,u)e.save!endendend用法Event.first.invite([1,2,3])我不认为以上是完成我的任务的最有效方法。我设想了一种方法,例如Model.find_or_cr
我有一个ActiveRecord对象,我想在不对模型进行永久验证的情况下阻止它被保存。您过去可以使用errors.add执行类似的操作,但它看起来不再有效了。user=User.lastuser.errors.add:name,"namedoesn'trhymewithorange"user.valid?#=>trueuser.save#=>true或user=User.lastuser.errors.add:base,"myuniqueerror"user.valid?#=>trueuser.save#=>true如何在不修改用户对象模型的情况下防止将用户对象保存在Rails3.2中